3GL - .   4GL -        

Scala

  1. Scala
  2. Scala
  3. Scala Java
  4. Scala
  5. :
  6. XML Scala
  7. Scala
  8. Scala
  9. Scala
  10. , Scala
  11. Scala
  12. Scala

Scala

; . Scala - , , , Java . Scala Java, Java-.

2000- Scala Java. Java ,  - (  [11]). Scala : , < >, Java, .

- , : . , (); (); , ().

Scala JVM, Java, , Java 5 (. ., Blackberry). Java Scala ; . ; , Tomcat, Scala.

Scala

Scala - , - - . Java, Scala : -, . , , , , , , Scala. Scala , -98 ( higher-ranking types, . . ).

Scala Java

, Scala Java . : . (, ,  - ? : JIT .   , 2.8.0, ; .)

Java- Scala, . , Java Scala , Java- Scala , Scala, , . < Java>, .

, Scala c Java, Scala- Java- - . , Scala , Java, Guice, Hibernate.

Scala Java-, Array; . Scala , .

Java- Scala (trait); ,  - . , , . . , .

; . , , . Scala, - (companion object) c , :

class A {...} object A { def myStaticMethod(s:String) : Integer {...} }

- .

<==> Scala , , Java. equals() null; () Scala eq, AnyRef.

: var s : String. def:

def square(n : Int) = n * n

Scala (generics):

var m : Map[String, List[Timestamp]] ... val s = m("today")(5)

( "today", .)

Java, (. [9]):

trait Functor[F[_]] { def fmap[A, B](fa: F[A], f: A => B): F[B] }

Java (c., , [8]). :

interface Functor<F<?> extends Collection<>> { public <A, B> F<B> fmap(A a, Function<A, B> f); }

Java; while , , , :

def main(args: Array[String]) { for (arg <- args) println(arg + ": " + calculate(arg)) }

:

for (x <- expr1; if expr2; y <- expr3) ...

, , :

for (x <- expr1) { if (expr2) { for (y <- expr3) ... } }

 - (closures). , Java - , , . Scala :

List("abc.scala", "xyz.py", "file.sh", "myclass.java"). filter (name => name.endsWith(".java"))

, Java-  - ; , Java Scala. , Scala , Java.

( Java) - , - : .

Java , int long String - Scala . , , , , :

implicit def randomAccess (s:String) = new RandomAccessSeq[Char] { def length = s.length def apply(i: Int) = s.charAt(i) }

val c = "This is a string"(2) - RandomAccessSeq[Char],  c  'i'. .



, :

// returns true if string contains a digit "abc123" exists (_.isDigit)

Scala . , , - ; Scala . :

class Human(name: String, sex: Sex) { def ping() { println("?!") } } class Russian(firstName: String, patronymic: String, lastName: String, sex: Sex) extends Human(firstName + " " + patronymic + " " + lastName, sex) { ... } trait Programmer { override def ping() { println("I'm busy!") } def debug(byte[] binary) { println("omg...") } } class SovietProgrammer(firstName: String, patronymic: String, lastName: String, birthDate: Timestamp) extends Russian(firstName, patronymic, lastName, null) with Programmer { ... }

debug() ping Programmer.

Scala

, Scala JVM < >, . , , , :

def myFun(x:Integer) = "Look, " + x + " * " + x + " = " + (x * x) val sameThing = (x: Integer) => "Look, " + x + " * " + x + " = " + (x * x) List(1,2,3) map {x => x * x * x} List(0., 3.14159265358) map { sin(_) } def const[X, Y](y: Y) = (x: X) => y def c123 = const(123) val x = c123("abc") // it is 123

, . Scala , :

val list = "Clatto" :: "Verata" :: "Nicto" :: Nil // Same as List("Clatto", "Verata", "Nicto") val list = List("double", "double") ::: List("toil", "and", "trouble") // concatenation list.exists(x => x.toString.length == 4) // wtf method of looking for TRUE list.drop(2).dropRight(4).filter(s => (s endsWith "y")) list.map(s => "\"" + s + "\"") // list comprehension List(1,2).flatMap(List(123,_,456)) // lists built by List(123,_,456) are concatenated list.foreach(print) list.reverse list.head list.tail list.last

, , :

val list = List(1,2,3,4,5) // folds the list, adding up the elements: list.foldLeft(0)(_+_)

( n-); data types, - , . - , - .

:

val x = (123, "abc", new Date)

:

abstract class Option[T] case class None[T]() extends Option[T] case class Some[T](value: T) extends Option[T]

case class - , , switch. ; , case-? , ; .


:

def sum(x:Int)(y:Int) = x + y sum(1)(2)

sum ,  (x) ,  (y). sum(5), <>.

(type classes) , :

trait Functor[F[_]] { def fmap[A, B](fa: F[A], f: A => B): F[B] }

, < Java>, : - .

Scala val - . var - , Java. val :

lazy val x = buildBigThingThatTakesLong(parameters)

, , .

:

def x = buildBigThingThatTakesLong(parameters)

: ,  - .



Scala (pattern matching; . ??) .

? :

val second : List[Int] => Int = { case x::y::_ => y }


:

val second = new PartialFunction[List[Int], Int] { def apply(xs: List[Int]) = xs match { case x :: y :: _ => y } def isDefinedAt(xs: List[Int]) = xs match { case x :: y :: _ => true case _ => false }

( x::y::z::Nil List(xyz)).

-:

val pf : PartialFunction[X,Y] = ... val y = pf(x) // exception happens if pf is not defined on the value of x val yOpt : Option[Y] = pf.lift(x) // returns either Some(y) or None val b : Boolean = pf.isDefinedAt(x) val pfAlt : PartialFunction[X,Y] = ... val y = pf.orElse(pf1) // if (pf.isDefinedAt(x)) pf(x) else pf1(x)



, : , -;  - , ? , ,

class A(i:Integer) { def f1(j: Integer, k: Integer) = i+j+k val f2 = (j:Integer, k: Integer) => i+j+k }

, f1 - , f2 - . f1 f2? val f3 = f1? , . (. ., Function), , , :

val f3 = f1 _

, , <>, , f1, ; f3 f1. f1 , , (closure). f3 ; .



C : , . (Stream). , , :

def sieve(s: Stream[Int]): Stream[Int] = Stream.cons(s.head, sieve(s.tail filter {_ % s.head != 0})) def primes = sieve(Stream from 2) primes take 100 foreach println



- (DSL), . , [5]:

val orders = List[Order]( // use premium pricing strategy new Order to buy(100 sharesOf "IBM") maxUnitPrice 300 using premiumPricing, // use default pricing strategy new Order to buy(200 sharesOf "GOOGLE") maxUnitPrice 300 using defaultPricing, // use custom pricing strategy new Order to sell(200 bondsOf "Sun") maxUnitPrice 300 using { (qty, unit) => qty * unit - 500 }

, - Scala, . , .

:

Scala - . Java - .

. , )switch/case:

def matchMe (s: String): Int = s match { case "ichi" => 1 case "ni" => 2 case "san" => 3 case _ => -1 }

Java 2007 switch : .

Scala , , , :

trait Expr { case class Num(value : int) extends Expr case class Var(name : String) extends Expr case class Mul(lft : Expr, rgt : Expr) extends Expr } ... // Simplification rule: expr match { case Mul(x, Num(1)) => x case _ => e }

C , , :

scala> val Entry = """(\w+)\s*=\s*(\d+)""".r Entry: scala.util.matching.Regex = (\w+)\s*=\s*(\w+) scala> def parse(s: String) = s match { case Entry(x,y) => (x, y) } parse: (s: String)(String, Int) scala> parse("salary=120") res50: (String, Int) = (salary,120)

, , . , -, .

// a regular tree, not binary, Tree[X] = X + Tree[X]^n abstract class Tree[X] // main class for a tree case class Leaf[X](v:X) extends Tree[X] case class Branch[X](kids:List[Tree[X]]) extends Tree[X] class TreeParser extends JavaTokenParsers { // A tree is either a leaf or a branch private def node : Parser[Tree[String]] = leaf | branch // a branch is a sequence of trees in parentheses; // postprocessing consists of calling a constructor private def branch : Parser[Branch[String]] = "("~repsep(node, ",")~")" ^^ { case "("~nodes~")" => Branch(List() ++ nodes) } // a leaf is a string wrapped in a constructor private def leaf : Parser[Leaf[String]] = string ^^ {case s => Leaf(s)} // a string is a string is a string private def string : Parser[String] = regex("""\w+""".r) // this is all that's exposed def read(input: CharSequence) = parseAll(node, input).get } def parseTree(input: CharSequence) = (new TreeParser).read(input) scala> parse("(a,(b,(c,de,(),f)),g)") res58: Tree[String] = Branch(List(Leaf(a), Branch(List(Leaf(b), Branch(List(Leaf(c), Leaf(de), Branch(List()), Leaf(f))))), Leaf(g)))

XML Scala

(, , ) , XML Scala , . :

class Person(val id: Int, val firstName: String, val lastName: String, val title: String) { def toXML = { <person> <id>{id}</id> <firstName>{firstName}</firstName> <lastName>{lastName}</lastName> <title>{title}</title> </person> } }

Scala

, , (. ., ), . . :

def qsort(list: List[Int]): List[Int] = list match { case Nil => Nil case x::xs => qsort(xs.filter(_ < x)) ::: x :: qsort(xs.filter(_ >= x)) }

, , , , ; , ; , ; , .



, :

> class Fact (n: Int) { > def fact(n: Int): BigInt = if (n == 0) 1 else fact(n-1) * n > def ! = fact(n) > } > implicit def int2fact(n: Int) = new Fact(n) > > println(42!) 1405006117752879898543142606244511569936384000000000

? 42!; . . ! Fact, Integer Fact - (int2fact), . ! - BigInt; println().

Scala

: scalatest ([12]) scalacheck. scalatest:

import org.scalatest.FlatSpec import org.scalatest.matchers.ShouldMatchers class StackSpec extends FlatSpec with ShouldMatchers { "A Stack" should "pop values in LIFO order" in { val stack = new Stack[Int] stack.push(1) stack.push(2) stack.pop() should equal (2) stack.pop() should equal (1) } it should "throw Exception on popping empty stack" in { val emptyStack = new Stack[String] evaluating { emptyStack.pop() } should produce [NoSuchElementException] } }

scalacheck :

val stackIsLIFO = forall { (stack: Stack[Int], x: Int, y: Int) => stack.push(x) stack.push(y) (y != stack.pop) |: "stack top should pop" && (x != stack.pop) |: "stack bottom lost" }

scalacheck , . , () - .

-, JUnit:

def testParse_positive_5 { val stack = new Stack[Int] stack.push(1) stack.push(2) assert(2 == stack.pop()) assert(1 == stack.pop()) assert(stack.isEmpty) }

Scala

Scala http://www.scala-lang.org/downloads; , , repl-, . Scala IntelliJ: ,  - , , . 2.8 Eclipse ; Netbeans.

, Lift, Scala, - .

Lift, -, , , , . , MVC; , , : . Scala XML, JSP, - .

, Scala:



HTML Scala:

<table> <lift:Show.users> <tr> <td><f:first_name>David</f:first_name></td> <td><f:last_name>Pollak</f:last_name></td> </tr> </lift:Show.users> </table> class Show { def users(xhtml: NodeSeq) = Users.findAll.flatMap(user => bind("f", xhtml, "first_name" -> user.firstName, "last_name" -> user.nameName)) }

, Scala

 - foursquare.com, - , , . ScalaLift.

Yammer: Artie, , Scala.

Twitter: Scala, Ruby on Rails.

LinkedIn: Scala-, , , , ,  - .

KaChing: Scala, Java; .

-, Scala; Dice, , Scala, , .

Scala

David Pollack  [10] - . Scala, .

Martin Oderski, Lexi Spoon, Bill Venners  [13]. , , . [14] - .

Dean Wampler, Alex Payne  [1]. , Scala, . < > .

[18], [16], [17] - , .

[15] - < Scala> .

[9] - ScalaZ, (Tony Morris). .

, , - Gregory Meredith  [7].  - , . : , .

Derek Chen-Becker, Tyler Weir, Marius Danciu  [6]. ScalaLift. Lift , , : [4], [3], [2].

Scala

[1]
Dean Wampler Alex Payne. Programming Scala: Scalability = Functional Programming + Objects, http://programming-scala.labs.oreilly.com/. O'Reilly, 2009.
[2]
Derek Chen-Becker. Lift - << >>. , http://github.com/tjweir/pocketchangeapp/tree/master/PocketChange.
[3]
Marius Danciu David Pollak, Derek Chen-Becker and Tyler Weir. Starting with Lift. -, http://old.liftweb.net/docs/getting_started/mod_master.html.
[4]
David Pollack et al. Getting Started With Lift. -, http://liftweb.net/getting_started.
[5]
Debashish Ghosh. Designing Internal DSLs in Scala. , http://debasishg.blogspot.com/2008/05/designing-internal-dsls-in-scala.html.
[6]
Tyler Weir Marius Danciu, Derek Chen-Becker. The Definitive Guide to Lift: A Scala-based Web Framework, http://www.amazon.com/Definitive-Guide-Lift-Scala-based-Framework/dp/1430224215. Apress, 2007.
[7]
Gregory Meredith. Pro Scala: Monadic Design Patterns for the Web, http://www.amazon.com/Pro-Scala-Monadic-Design-Patterns/dp/143022844X. Apress, 2010?
[8]
JP Moresmaugh. Java and higher order generics. , http://jpmoresmau.blogspot.com/2007/12/java-and-higher-order-generics.html.
[9]
Tony Morris. scalaz. Google Code, http://code.google.com/p/scalaz/.
[10]
David Pollack. Beginning Scala, http://www.amazon.com/Beginning-Scala-David-Pollak/dp/1430219890. Apress, 2009.
[11]
David Rupp. Java generics broken? we report, you decide. , http://davidrupp.blogspot.com/2008/01/java-generics-broken-we-report-you.html.
[12]
Bill Venners. Scalatest. , http://www.scalatest.org/.
[13]
Bill Venners, Martin Odersky, and Lexi Spoon. Programming in Scala: A Comprehensive Step-by-step Guide, http://www.amazon.com/Programming-Scala-Comprehensive-Step---step/dp/0981531601/. Artima, 2008.
[14]
, , . Scala. RSDN, http://www.rsdn.ru/article/scala/scala.xml.
[15]
. Scala. RSDN, http://www.rsdn.ru/article/philosophy/Scala.xml.
[16]
. Scala: Actors (part 2). , http://blog.apanasenko.me/2009/12/scala-actors-part-2/.
[17]
. Scala: Functional Language (part 3). , http://blog.apanasenko.me/2009/12/scala-functional-language-part-3/.
[18]
. Scala: FL JVM (part 1). , http://blog.apanasenko.me/2009/12/scala-fl-jvm-part-1/.

 

          3GL - .   4GL -        

, - , , , . .




 10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _.
10.11.2021 - 12:36: - Conscience -> . ? - _.
10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _.
10.11.2021 - 12:35: - Ecology -> - _.
10.11.2021 - 12:34: , - War, Politics and Science -> - _.
10.11.2021 - 12:34: , - War, Politics and Science -> . - _.
10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _.
10.11.2021 - 09:18: - New Technologies -> , 5G- - _.
10.11.2021 - 09:18: - Ecology -> - _.
10.11.2021 - 09:16: - Ecology -> - _.
10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _.
10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _.
Bourabai Research -  XXI Bourabai Research Institution