「Programming in Scala」の読書、二章
積読のProgramming in Scala消化中...
流し読みは、21章辺りまで終ってます。けど流し読みでも後半は読むのがきついので一通り読み終わるのはいつになるのやらw;
Chapter 2 First Steps in Scala
Step1. Learn to use the Scala interpreter
replで確認^^
scala> 1+2 res1: Int = 3
Java's primitive have corresponding Scala's class.
scala> res1 * 3 res2: Int = 9
これは、知らなかった*A*
Step2. Define some variables
scala> val msg = "Hello World!" //=>type inference msg: java.lang.String = Hello World! scala> val msg2 : java.lang.String = "Hello again, world!" msg2: java.lang.String = Hello again, world! scala> val msg3 : String = "Hello yet again, world!" msg3: String = Hello yet again, world! scala> println(msg) Hello World! scala> msg = "Goodbye cruel world!" <console>:5: error: reassignment to val msg = "Goodbye cruel world!" scala> var greeting = "Hello world" greeting: java.lang.String = Hello world scala> greeting = "Leave me alone, world!" greeting: java.lang.String = Leave me alone, world!
Step3. Define some functions
scala> def max(x: Int, y: Int): Int = if(x > y) x else y max: (Int,Int)Int scala> def max3(x: Int, y: Int, z: Int): Int = if(x > y && x > z) x else max3(y,z,x) max3: (Int,Int,Int)Int scala> max3(1,2,3) res6: Int = 3 scala> max3(2,3,1) res7: Int = 3 scala> max3(3,1,2) res8: Int = 3
paren //=> ()
blanket? //=> []
curly braces //=> {}
scala> def max2(x: Int, y: Int) = if(x>y) x else y max2: (Int,Int)Int
scala> def greet() = println(msg)
greet: ()Unit
引数なしの戻り値なし
scala> quit
Step4. Write some Scala scripts
Step5. Loop with while; decide with if
配列をindexで操作するのはよろしくないとの事
Step6. INterate with foreach and for
scala> val ary = Array("Concise", "is", "nice") ary: Array[java.lang.String] = Array(Concise, is, nice) scala> ary.foreach( a => println(a)) //=>type inference Concise is nice scala> ary.foreach((a : String) => println(a)) //=>normal Concise is nice scala> ary.foreach(println(_)) //=>concise Concise is nice scala> ary.foreach(println) Concise is nice
forの場合
scala> for(a <- ary)
| println(a)
Concise
is
nice
conclusion
scalaの基本を完了っと^^;
う〜ん、まだこの辺は読みやすいんだよね。って後半は多分日本語になっていても理解しづらい部分だからしょうがないけどw;
Programming in Scala: A Comprehensive Step-by-step Guideposted with amazlet at 09.06.28Martin Odersky Lex Spoon Bill Venners
Artima Inc
売り上げランキング: 8148