REPLでの対話の例(コメントつき)

Welcome to Scala version 2.11.4 (Java HoTM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions to have them evaluatType :help for more information.

// *** べき乗

scala> Math.pow(2,2)
res0: Double = 4.0
scala> Math.pow(8,8)
res1: Double = 1.6777216E7

scala> Math.PI
res3: Double = 3.141592653589793
scala> 90 / 180 * Math.PI
res4: Double = 0.0      // 整数の除算をしてしまうので0になった
    // 順序を変えることで対処できる
scala> 90  * Math.PI / 180
res5: Double = 1.5707963267948966
scala> 120  * Math.PI / 180
res6: Double = 2.0943951023931953
scala> 180  * Math.PI / 180
res7: Double = 3.141592653589793

// *** ループ

scala> for(x <- 1 to 10) println(x)
1
2
3
4
5
6
7
8
9
10

scala> for(i <- 1 to 10) println(i)
1
2
3
4
5
6
7
8
9
10

scala> for(i <- 1.0 to 3 by 0.2) println(i)
1.0
1.2
1.4
1.5999999999999999
1.7999999999999998
1.9999999999999998
2.1999999999999997
2.4
2.6
2.8000000000000003
3.0000000000000004

// *** タブ文字を含む文字列

scala> println("abcdefg\thijklmn\topqursu")
abcdefg hijklmn opqurstu
scala> println("abcd\thijklmn\topqurstu")
abcd    hijklmn opqurstu
scala> println("abcd\thijk\topqurstu")
abcd    hijk    opqurstu
scala> println("abd\thjk\topqurstu")
abd     hjk     opqurstu

scala> "1\t2"
res17: String = 1       2
scala> println("1\t2")
1       2

scala> for(n<-1 to 10)   println(n+"\t"+f(n))
1       1.0
2       4.0
3       9.0
4       16.0
5       25.0
6       36.0
7       49.0
8       64.0
9       81.0
10      100.0

scala> def f(x:Double)=x*x
f: (x: Double)Double

scala> for(n<-1 to 10)   println(n+","+f(n))
1,1.0
2,4.0
3,9.0
4,16.0
5,25.0
6,36.0
7,49.0
8,64.0
9,81.0
10,100.0
scala> for(n<-1 to 10)   println(n+", "+f(n))
1, 1.0
2, 4.0
3, 9.0
4, 16.0
5, 25.0
6, 36.0
7, 49.0
8, 64.0
9, 81.0
10, 100.0
scala> for(n<-1 to 10)   println(n+" : "+f(n))
1 : 1.0
2 : 4.0
3 : 9.0
4 : 16.0
5 : 25.0
6 : 36.0
7 : 49.0
8 : 64.0
9 : 81.0
10 : 100.0
scala> for(n<-1 to 10)   println(n+"\t"+f(n))
1       1.0
2       4.0
3       9.0
4       16.0
5       25.0
6       36.0
7       49.0
8       64.0
9       81.0
10      100.0

// ***

scala> 1
res24: Int = 1
scala> 1.toString
res25: String = 1

scala> s"ab\n${1+2}c"
res2: String =
ab
3c

// *** f変換

scala> f"${1}%2d"
res5: String = " 1"
scala> f"${1.0}%3f"
res6: String = 1.000000
scala> f"${1.0}%3.3f"
res7: String = 1.000

scala> for(x<-1.0 to 2.0 by 0.1) println(f"$x%3.3f\t${x*x}%3.3f")
1.000   1.000
1.100   1.210
1.200   1.440
1.300   1.690
1.400   1.960
1.500   2.250
1.600   2.560
1.700   2.890
1.800   3.240
1.900   3.610
2.000   4.000

scala> for(n<-1 to 10)   println(f"${n}%3d\t${f(n)}%-3.3f")
  1     1.000
  2     4.000
  3     9.000
  4     16.000
  5     25.000
  6     36.000
  7     49.000
  8     64.000
  9     81.000
 10     100.000

// *** Collection を使った方法(まだ解説していない)

scala> f"${10}%f"
res51: String = 10.000000

scala> f"${10}%5.2f"
res52: String = 10.00

scala> f"${10}%10.2f"
res53: String = "     10.00"

scala> (1.0 to 2 by 0.2).map((e:Double)=>{e*e})
res57: scala.collection.immutable.IndexedSeq[Double] = Vector(1.0, 1.44, 1.9599999999999997, 2.5599999999999996, 3.2399999999999993, 3.999999999999999)

scala> (1.0 to 2 by 0.2).map((e:Double)=>e*e)
res58: scala.collection.immutable.IndexedSeq[Double] = Vector(1.0, 1.44, 1.9599999999999997, 2.5599999999999996, 3.2399999999999993, 3.999999999999999)

scala> (1.0 to 2 by 0.2).map((e:Double)=>Math.pow(e,2))
res60: scala.collection.immutable.IndexedSeq[Double] = Vector(1.0, 1.44, 1.9599999999999997, 2.5599999999999996, 3.2399999999999993, 3.999999999999999)

scala> (1.0 to 2 by 0.2).map(Math.pow(_,2))
res61: scala.collection.immutable.IndexedSeq[Double] = Vector(1.0, 1.44, 1.9599999999999997, 2.5599999999999996, 3.2399999999999993, 3.999999999999999)

scala> (1.0 to 2 by 0.2).map((e)=>Math.pow(e,2))
res62: scala.collection.immutable.IndexedSeq[Double] = Vector(1.0, 1.44, 1.9599999999999997, 2.5599999999999996, 3.2399999999999993, 3.999999999999999)

scala> (1.0 to 2 by 0.2).map((e)=>f"${e}%3.3f\t${Math.pow(e,2)}%8.3f")
res64: scala.collection.immutable.IndexedSeq[String] = Vector(1.000        1.000, 1.200    1.440, 1.400    1.960, 1.600
   2.560, 1.800    3.240, 2.000    4.000)

scala> (1.0 to 2 by 0.2).map((e)=>f"${e}%3.3f\t${Math.pow(e,2)}%8.3f").foreach(println)
1.000      1.000
1.200      1.440
1.400      1.960
1.600      2.560
1.800      3.240
2.000      4.000

scala>