6. Java public boolean hasUpperCase ( String text ) { boolean hasUpperCase = false ; for ( int i = 0; i < text . length (); i ++) { if ( Character . isUpperCase ( text . charAt ( i ))) { hasUpperCase = true ; break ; } } return hasUpperCase ; }
7. Scala def hasUpperCase ( text : String ) = text . exists ( _ . isUpperCase )
9. Java public BigInteger ????? ( BigInteger x ) { if ( x == BigInteger . ZERO ) { return BigInteger . ONE ; } return x . multiply ( ?????( x . subtract ( BigInteger . ONE ))); }
10. Java public BigInteger factorial ( BigInteger x ) { if ( x == BigInteger . ZERO ) { return BigInteger . ONE ; } return x . multiply ( factorial ( x . subtract ( BigInteger . ONE ))); }
11. def factorial ( x : BigInt ) = if ( x == 0) 1 else x * factorial ( x - 1) Scala
12. Java public class Person { private String name ; private int age ; public Person ( String name , int age ) { this . name = name ; this . age = age ; } public String getName () { return name ; } public int getAge () { return age ; } public void setName ( String name ) { this . name = name ; } public void setAge ( int age ) { this . age = age ; } }
13. class Person ( var name : String , var age : int ) Scala
14. Turtles all the way down http://en.wikipedia.org/wiki/Turtles_all_the_way_down
17. def users = < users > < user role = "customer" > < name >{ user . name }</ name > < password >{ user . password }</ password > < email >{ user . email }</ email > </ user > <user role= "boss" > < name >{ boss . name }</ name > < password >{ boss . password }</ password > < email >{ boss . email }</ email > </ user > </ users > users "user"
18. DSLs Growing a language http://video.google.com/videoplay?docid=-8860158196198824415
19. Scala specs object HelloSpec extends Specification { "'hello world' has 11 characters" in { "hello world" size must_== 11 } "'hello world' matches 'h.* w.*'" in { "hello world" must be matching ( "h.* w.*" ) } }
20. object Basic extends Baysick { def main ( args : Array [ String ]) = { 10 PRINT "Welcome to Baysick Lunar Lander v0.9" 20 LET ( 'dist := 100) 30 LET ( 'v := 1) 40 LET ( 'fuel := 1000) 50 LET ( 'mass := 1000) 60 PRINT "You are drifting towards the moon." 70 PRINT "You must decide how much fuel to burn." 80 PRINT "To accelerate enter a positive number" 90 PRINT "To decelerate a negative" 100 PRINT "Distance " % 'dist % "km, " % "Velocity " % 'v % "km/s, " % "Fuel " % 'fuel 110 INPUT 'burn 120 IF ABS ( 'burn ) <= 'fuel THEN 150 130 PRINT "You don't have that much fuel" 140 GOTO 100 150 LET ( 'v := 'v + 'burn * 10 / ( 'fuel + 'mass )) 160 LET ( 'fuel := 'fuel - ABS ( 'burn )) 170 LET ( 'dist := 'dist – 'v ) 180 IF 'dist > 0 THEN 100 190 PRINT "You have hit the surface" 200 IF 'v < 3 THEN 240 210 PRINT "Hit surface too fast (" % 'v % ")km/s" 220 PRINT "You Crashed!" 230 GOTO 250 240 PRINT "Well done" 250 END RUN } } http://www.scala-lang.org/node/1403
21. val worldSaver = actor { loop { react { case SaveTheWorld ( when ) => saveTheWorldAfter ( when ) case BeBad => // Ignore it - we are good! } } } worldSaver ! SaveTheWorld ( new Date )
24. Функции ( x : Int , y : Int ) => if ( x > y ) x else y val max = ( x : Int , y : Int ) => if ( x > y ) x else y max (1, 4) // 4
25. val numbers = Array (1, 2, 3, -1) numbers . map (( n ) => n * 2) // Array(2, 4, 6, -2) numbers . map ( _ * 2) int [] doubled = new int [arr. length ]; for ( int i = 0; i < arr. length ; i++) { doubled[i] = arr[i] * 2; }
26. public boolean hasUpperCase ( String text ) { boolean hasUpperCase = false ; for ( int i = 0; i < text . length (); i ++) { if ( Character . isUpperCase ( text . charAt ( i ))) { hasUpperCase = true ; break ; } } return hasUpperCase ; } def hasUpperCase ( text : String ) = text . exists ( _ . isUpperCase )
28. arg match { case "-h" | "-help" => println ( "Params: -help|-verbose" ) case "-v" | "-verbose" => verbose = true }
29. val list = 1 :: 2 :: 3 :: Nil list match { case Nil => // Empty list case head :: tail => // head = 1, tail = List(2, 3) }
30. val tuple = (1, "asd" ) val ( one , asd ) = tuple
31. case class Person ( firstName : String , lastName : String ) val johnDoe = Person ( "John" , "Doe" ) val Person ( john , doe ) = johnDoe
33. Първи дубъл def qsort ( items : List [ Int ]) : List [ Int ] = { if ( items . length <= 1) return items var left : List [ Int ] = Nil var middle : List [ Int ] = Nil var right : List [ Int ] = Nil val pivot = items . head for ( item <- items ) { if ( item < pivot ) { left = item :: left } else if ( item > pivot ) { right = item :: right } else { middle = item :: middle } } qsort ( left ) ::: middle ::: qsort ( right ) }
34. Редно(2) дубъл def qsort ( items : List [ Int ]) = { items match { case Nil => Nil case xs => { val pivot = xs . head qsort ( xs . filter ( _ < pivot )) ::: xs . filter ( _ == pivot ) ::: qsort ( xs . filter ( _ > pivot )) } } }
38. trait Phone { def dial ( number : Int ) // Abstract } trait AnalogPhone extends Phone { def dial ( number : Int ) { /* rotate */ } } trait DigitalPhone extends Phone { def dial ( number : Int ) { /* send 0101101 */ } }
39. trait Fax { this: Phone => def fax ( number : Int ) { dial ( number ) /* from phone */ /* send fax */ } }
40. class DigitalFax extends Fax with DigitalPhone val analogFax = new Fax with AnalogPhone
41. class CoffeeMachine { def makeCoffee () = "a nice cup of coffee" } val allInOne = new CoffeeMachine with Fax with DigitalPhone with Copier /* with Printer with Scanner */
43. trait Fax { this: Phone => def fax ( number : Int ) { dial ( number ) /* from phone */ /* send fax */ } } val analogFax = new Fax with Analog class DigitalFax extends Fax with DigitalPhone