REPLでのインタラクションの例

C:\Users\NEC-PCuser > scala

Welcome to Scala version 2.11.4 (Java HotS
pot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions to have them evaluated
.
Type :help for more information.


scala> :h
h is ambiguous: did you mean :help or :his
tory or :h??

scala> :help
All commands can be abbreviated, e.g. :he inelp.
:edit <id>|<line>        edit history
:help [command]          print this summary specific help
:history [num]           show the history (o is commands to show)
:h? <string>             search the history :imports [name name ...] show import historyng sources of names
:implicits [-v]          show the implicits :javap <path|class>      disassemble a file me
:line <id>|<line>        place line(s) at thstory
:load <path>             interpret lines in :paste [-raw] [path]     enter paste mode orle
:power                   enable power user m:quit                    exit the interprete:replay [options]        reset the repl and previous commands
:reset [options]         reset the repl to istate, forgetting all session entries
:save <path>             save replayable sesile
:sh <command line>       run a shell command implicitly => List[String])
:settings <options>      update compiler optssible; see reset
:silent                  disable/enable autoing of results
:type [-v] <expr>        display the type ofion without evaluating it
:kind [-v] <expr>        display the kind of's type
:warnings                show the suppressedrom the most recent line which had any
scala> (1 to 20)
res0: scala.collection.immutable.Range.Inclue(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,, 17, 18, 19, 20)

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

scala> sq(2)
res1: Double = 4.0

scala> sq(100)
res2: Double = 10000.0

scala> ((a:Double)=>{a*2})
res4: Double => Double = <function1>

scala> (1 to 100)
res5: scala.collection.immutable.Range.Inclue(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 270, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 444, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,  58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 825, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 999, 100)

scala> (1 to 100).sum
res6: Int = 5050

scala> (1 to 100).product
res7: Int = 0

scala> (1 to 100).max
res8: Int = 100

scala> (1 to 100).min
res9: Int = 1

scala> for(i<-1 to 10)yield i
res10: scala.collection.immutable.IndexedSeqtor(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala>

scala> (1 to 10).map{_*2}
res11: scala.collection.immutable.IndexedSeqtor(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> (1 to 10).map{(x)=>x*2}
res12: scala.collection.immutable.IndexedSeqtor(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> (1 to 10).map{(x)=>{x*2}}
res13: scala.collection.immutable.IndexedSeqtor(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> (1 to 10).map((x)=>{x*2})
res14: scala.collection.immutable.IndexedSeqtor(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> (1 to 10).map((xyz)=>{xyz*2})
res15: scala.collection.immutable.IndexedSeqtor(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> (1 to 10).map((x)=>{1;2;3;x;x*2})
<console>:8: warning: a pure expression does statement position; you may be omitting necntheses
              (1 to 10).map((x)=>{1;2;3;x;x*
                                  ^
<console>:8: warning: a pure expression does statement position; you may be omitting necntheses
              (1 to 10).map((x)=>{1;2;3;x;x*
                                    ^
<console>:8: warning: a pure expression does statement position; you may be omitting necntheses
              (1 to 10).map((x)=>{1;2;3;x;x*
                                      ^
<console>:8: warning: a pure expression does statement position; you may be omitting necntheses
              (1 to 10).map((x)=>{1;2;3;x;x*
                                        ^
res16: scala.collection.immutable.IndexedSeqtor(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> (1 to 10).map((x)=>x*2)
res17: scala.collection.immutable.IndexedSeqtor(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> (1 to 10).map((x:Int)=>x*2)
res19: scala.collection.immutable.IndexedSeqtor(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> (1 to 10).map(x=>x*2)
res20: scala.collection.immutable.IndexedSeqtor(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> (1 to 10).map(_*2)
res21: scala.collection.immutable.IndexedSeqtor(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> (1 to 10).reduce((maenoatai,imanoataii*imanoatai)
res22: Int = 3628800

scala> (1 to 10).reduce((a,b)=>a*b)
res23: Int = 3628800

scala> (1 to 10).reduce((x,y)=>x*y)
res24: Int = 3628800

scala> (1 to 10).reduce((x,y)=>x+y)
res25: Int = 55

scala> (1 to 10).fold(""){(x,y)=>x+y.toStrinres28: Any = 12345678910

scala> (1 to 10).fold(""){(x,y)=>x+y.toString}
res30: Any = 12345678910

scala> (1 to 10).fold(""){(x,y)=>x+"_"+y.toSres31: Any = _1_2_3_4_5_6_7_8_9_10

scala> (1 to 10).fold("0"){(x,y)=>x+"_"+y.tores32: Any = 0_1_2_3_4_5_6_7_8_9_10

scala> (1 to 10).foldRight("0"){(x,y)=>x+"_"}
res33: String = 1_2_3_4_5_6_7_8_9_10_0

scala> for(i<-1 to 20)yield math.random
res34: scala.collection.immutable.IndexedSeqVector(0.8190699164089229, 0.31998480248190552507395948, 0.06615071235988479, 0.72533723.9039878800165853, 0.7669710402496198, 0.28428, 0.4407536358151214, 0.34806962543053555,4095426, 0.38488696063878114, 0.044337821498433988068638409, 0.15130730575646867, 0.81438, 0.11074994758392898, 0.16818004560702504,430846667, 0.17657526878119278)

scala> for(i<-1 to 20)yield (math.random*10)res35: scala.collection.immutable.IndexedSeqtor(1, 5, 9, 0, 0, 8, 2, 8, 6, 1, 5, 7, 4, 46, 2, 3)

scala> for(i<-1 to 20)yield (math.random*100res36: scala.collection.immutable.IndexedSeqtor(8, 67, 73, 72, 62, 98, 80, 71, 30, 26, 44, 71, 37, 94, 20, 43, 44)

scala> for(i<-1 to 20)yield (math.random*100res37: scala.collection.immutable.IndexedSeqVector(2.680434822219835, 45.88381955333599,6601982, 39.83730716337093, 13.2699974407769074259413, 64.90506046509043, 98.7387711297635536206306, 54.021995047608605, 86.111180732837700012182, 25.327719520791703, 61.2449277.39063858845176, 9.282367591303874, 0.141348, 21.329988240544274, 26.060111549509724, 209502)

scala> for(i<-1 to 20)yield (math.random*100res38: scala.collection.immutable.IndexedSeqtor(0, 59, 50, 15, 76, 97, 14, 59, 27, 38, 787, 50, 14, 43, 79, 63, 62)

scala> for(i<-1 to 20)yield math.random.*(10res39: scala.collection.immutable.IndexedSeqtor(88, 77, 92, 10, 23, 49, 13, 77, 15, 49,  53, 14, 37, 36, 53, 30, 43)

scala> for(i<-1 to 20)yield math.random.*(10res40: scala.collection.immutable.IndexedSeqtor(929, 60, 687, 536, 214, 485, 302, 786, 6, 808, 704, 374, 102, 127, 966, 839, 846, 50

scala> val ns=for(i<-1 to 20)yield math.random.*(1000).toInt
ns: scala.collection.immutable.IndexedSeq[Int] = Vector(315, 507, 267, 58, 352, 364, 552, 711, 391, 552, 474, 625, 280, 105, 823, 665, 397, 940, 975, 668)

scala> ns.size
res41: Int = 20

scala> ns.length
res42: Int = 20

scala> ns.reverse
res43: scala.collection.immutable.IndexedSeq[Int] = Vector(668, 975, 940, 397, 665, 823, 105, 280, 625, 474, 552, 391, 711, 552, 364, 352, 58, 267, 507, 315)

scala> ns.reverse.reverse.reverse
res44: scala.collection.immutable.IndexedSeq[Int] = Vector(668, 975, 940, 397, 665, 823, 105, 280, 625, 474, 552, 391, 711, 552, 364, 352, 58, 267, 507, 315)

scala> ns.reverse.reverse.reverse.reverse
res46: scala.collection.immutable.IndexedSeq[Int] = Vector(315, 507, 267, 58, 352, 364, 552, 711, 391, 552, 474, 625, 280, 105, 823, 665, 397, 940, 975, 668)

scala> ns.sum
res47: Int = 10021

scala> ns.sum/ns.size
res48: Int = 501

scala> ns.sum%ns.size
res49: Int = 1

scala> ns.sum.toDouble/ns.size
res50: Double = 501.05

scala> ns.sum+0.0
res51: Double = 10021.0

scala> ns.sum*1.0
res52: Double = 10021.0

scala>