Scala
Scala
Scala JVM
JVM
OS
VM OS
VM
1
Scala
Scala
Scala
// 1
/*
*
*/
println("Hello, world!") //
42 // Int
3.14 // Double
"hogehoge" // String
true // Boolean
() // Unit
Array(1, 2, 3) //
List(1, 2, 3) //
Map("a" -> 1, "b" -> 2) //
Set(1, 2, 3) //
1 :: 2 :: 3 :: Nil
// :: Nil
// List(1, 2, 3)
(42, "string", 3.14) //
s"$value ${value2}" // $ ${}
val i = 42 // val
// i = 99 // val
val j: Int = 100 //
// k: String = 100 //
var x = 42 //
x = 99 //
Scala if for match while
// if (cond) cond Boolean
if (cond) {
...
} else {
...
}
for (i <- seq) {
...
}
// Scala if for
val ret = if (false) {
"aaa"
} else if (true) {
"bbb"
} else {
"ccc"
}
ret // => "bbb"
val list = 1 :: 2 :: 3 :: Nil
val ret = for (i <- list) { i * 2 }
ret //=> Unit
// for
// list.foreach { i => ... }
val list = for (i <- list) yield { i * 2 }
list //=> List(2, 4, 6)
// yield yield
// list.map { i => ... }
val num = 42
// match
num match {
case n if n > 40 => println("over 40.")
case _ => println("less than or equal to 40.") // _
}
val tuple = (42, "answer")
val (i, s) = tuple
i // 42
s // tuple
// match
tuple match {
case (40, s) => println(s"40 is $s")
case (i, "universe") => println(s"$i is universe")
case (i, s) => println(s"$i is $s")
}
val tuple = (42, "hogefuga", 3.14)
tuple match {
case (40, "foobar", 2.718) => //
case (Num, Str, Point) => //
case (`num`, `str`, `p`) => //
case (n: Int, s: String, p: Int) => //
case tup @ (n, s, p) => //
}
val list = 1 :: 2 :: 3 :: Nil
// head tail
val head :: tail = list
head // 1
tail // List(2, 3)
list match {
case 42 :: tail => // 42
case head :: Nil => // Nil 1
case e1 :: e2 :: _ => // 2
case Nil => //
}
//
val tuple = (42, "answer", 1 :: 2 :: 3 :: Nil)
tuple match {
case (40, str, head :: Nil) =>
case (i, "answer", x :: xs) =>
case (_, _, list @ (head :: tail)) => // list List
case _ => //
}
Scala
def
def func(x: Int, y: Int): Int = {
x + y
}
//
// 1
def func2(x: Int, y: Int) = x + y
Scala
//
val fun = (x: Int, y: Int) => x + y
//
fun(1, 2) //=> 3
=>
// x y Int
val fun: (Int, Int) => Int = (x, y) => x + y
=>
match =>
=>
// =>
val fun: Int => String = (i: Int) => i match {
case 42 => "correct"
}
val fun = (x: Int, y: Int) = x + y
//
def fortytwo(num: Int, f: (Int, Int) => Int) = f(num, 42)
//
fortytwo(99, fun) // 141
//
fortytwo(2, (x, y) => x * y) // 84
// fortytwo x y
map
map
map
Scala List map
val list = 1 :: 2 :: 3 :: Nil
// 2
val fun = i: Int => i * 2
// map
list.map(fun) //=> List(2, 4, 6)
// List fun
//
list.map(i => i + 2) // List(3, 4, 5)
CLI
$ scala cli.scala 74 76 81 89 92 87 79 85
mean: 82
object Main {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
cli.scala
Hello, world!
$ scala cli.scala
Scala
sbt
Scala sbt
sbt
$ sbt
compile
run main
sbt
sbt target
1-1
Scala main
object Main {
def main(args: Array[String]): Unit = {
for (arg <- args) {
println(arg)
}
}
}
args.scala
$ scala args.scala hoge fuga
hoge
fuga
$ scala args.scala 42 99
42
99
2-1
Scala 2.11 scala.io.StdIn
object Main {
def main(args: Array[String]): Unit = {
val input = scala.io.StdIn.readLine
println(input)
}
}
stdin.scala
$ scala stdin.scala
$ scala stdin.scala
Hello, world!
Hello, world!
split
object Main {
def main(args: Array[String]): Unit = {
val input = scala.io.StdIn.readLine.split(" ")
for (word <- input) println(word)
}
}
$ scala stdin.scala
hoge fuga piyo
hoge
fuga
piyo
2-2
Scala
……
42 + "99" // toString "4299"
Ruby JavaScript
Ruby
irb(main):001:0> 42 + "99"
TypeError: String can't be coerced into Fixnum
from (irb):1:in `+'
from (irb):1
JavaScript
> 42 + '99'
'4299'
> '7' * 9
63
> 42 * 'hoge'
NaN
Scala
NaN
Scala
implicit
toInt
toDouble
42 + "99".toInt // 141
3 * 3 * "3.14".toDouble // 28.26
2
object Main {
def main(args: Array[String]): Unit = {
for (arg <- args) {
println(arg.toInt * 2)
}
}
}
number.scala
$ scala number.scala 42 99 1
84
198
2
3-1
"hoge" toInt
toDouble
Array[String] ->
List[Double]
Array
Array List
toDouble
toList
Array toList List
args.toList
map
map
map
Double
args.map(_.toDouble)
Array[String] List[Double]
args.map(_.toDouble).toList
4-1
Array[String] List[Double]
Array List
val arr = Array("1", "2", "3")
val list = List(1.0, 2.0, 3.0)
CLI
/
sum
sum
Scala sum
sum
0
Scala Nil
Nil sum(Nil) 0
1 2
3 ……
n
n
n n n-1
n = 1 n-1
n n
1..n
1..n 1..n-1 n
1 + 2 + ... + n - 2 + n - 1 + n = (1 +
2 + ... n - 2 + n - 1) + n
3 :: 2 :: 7 :: 5 :: Nil
1 4 17 1 3
12 4 5
17
n
1
n - 1 = 0 0
n - 1 > 0
0
0
sum
0
n n 1..n-1
Scala
sum( 3 :: 2 :: 7 :: 5 :: Nil )
3 + sum( 2 :: 7 :: 5 :: Nil )
3 + 2 + sum( 7 :: 5 :: Nil)
3 + 2 + 7 + sum( 5 :: Nil)
3 + 2 + 7 + 5 + sum( Nil )
3 + 2 + 7 + 5 + 0
3 + 2 + 7 + 5
3 + 2 + 12
3 + 14
17
Scala
Scala
def sum(list: List[Double]): Double = ???
List[Double]
Double
if
match
def sum(list: List[Double]): Double =
list match {
case Nil => ???
case head :: tail => ???
}
0
def sum(list: List[Double]): Double =
list match {
case Nil => 0
case head :: tail => ???
}
def sum(list: List[Double]): Double =
list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
5-1
1 :: 2 :: 3 :: Nil match {
case a :: b :: rest =>
println(a) // 1
println(b) // 2
println(rest) // List(3)
}
1
length
sum
length
sum 0
length
sum
0
n
n n-1
1 1
n-1 1 n
n
n-1
sum 3 :: 2 :: 7
:: 5 :: Nil
length( 3 :: 2 :: 7 :: 5 :: Nil )
1 + length( 2 :: 7 :: 5 :: Nil )
1 + 1 + length( 7 :: 5 :: Nil )
1 + 1 + 1 + length( 5 :: Nil )
1 + 1 + 1 + 1 + length( Nil )
1 + 1 + 1 + 1 + 0
1 + 1 + 1 + 1
1 + 1 + 2
1 + 3
4
Scala
def length(list: List[Double]): Double =
list match {
case Nil => 0
case _ :: tail => 1 + length(tail)
}
_ Scala
_
6-1
_
_
mean
/
def mean(list: List[Double]): Double = sum(list) / length(list)
CLI
CLI
object Main {
def sum(list: List[Double]): Double =
list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
def length(list: List[Double]): Double =
list match {
case Nil => 0
case _ :: tail => 1 + length(tail)
}
def mean(list: List[Double]): Double = sum(list) / length(list)
def main(args: Array[String]): Unit = {
$ scala cli.scala
mean: 82.875
2
def sum(list: List[Double]): Double =
list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
def length(list: List[Double]): Double =
list match {
case Nil => 0
case _ :: tail => 1 + length(tail)
}
def common(list: List[Double]): Double =
list match {
case Nil => 0
case head :: tail => ??? + common(tail)
}
???
def common(list: List[Double], f: Double => Double): Double =
list match {
case Nil => 0
case head :: tail => f(head) + common(tail, f)
}
1
sum length
def sum(list: List[Double]): Double = common(list, head => head)
def length(list: List[Double]): Double = common(list, _ => 1)
head => head _ => 1
head => head 1
_ => 1 1 1
7-1
_ => 1 head => 1
_
match _
7-2
def test(fun: Double => Double) = fun(3.14)
test(_ => 1) // 1.0
1
val fun = _: Double => 1
test(fun) //
:
7-3
identity
fold
fold
common
fold
fold sum length
map filter
def foldr[A, B](f: (A, B) => B, ini: B, list: List[A]): B =
list match {
case Nil => ini
case head :: tail => f(head, foldr(f, ini, tail))
}
Double
length sum foldr
def length(list: List[Double]): Double =
foldr[Double, Double]((_, i) => i + 1, 0, list)
def sum(list: List[Double]): Double =
foldr[Double, Double]((a, i) => a + i, 0, list)
7-4
foldr
max
min
reverse
filter
map

学生向けScalaハンズオンテキスト