はわわーっ

はわわわわっ

scalaのリストを使ってみる

ちょっとリストの使い方の練習。

scala> List(1,2,3) ::: List(4,5)
res0: List[Int] = List(1, 2, 3, 4, 5)

scala> 5 :: List(1,2,3)
res1: List[Int] = List(5, 1, 2, 3)

scala> List(1,2,3,4,5).head
res2: Int = 1

scala> List(1,2,3,4,5).tail
res3: List[Int] = List(2, 3, 4, 5)

scala> List(1,2,3,4,5).last
res4: Int = 5

scala> List(1,2,3,4,5).init
res5: List[Int] = List(1, 2, 3, 4)

scala> List(1,2,3,4,5).length
res6: Int = 5

scala> List(1,2,3,4,5).reverse
res7: List[Int] = List(5, 4, 3, 2, 1)

scala> List(1,2,3,4,5).isEmpty
res8: Boolean = false

scala> List().isEmpty
res9: Boolean = true

scala> Nil.isEmpty
res10: Boolean = true

scala> List(1,2,3,4,5).take(2)
res11: List[Int] = List(1, 2)

scala> List(1,2,3,4,5).take(10)
res12: List[Int] = List(1, 2, 3, 4, 5)

scala> List(1,2,3,4,5).drop(2)
res13: List[Int] = List(3, 4, 5)

scala> List(1,2,3,4,5).sum
res18: Int = 15

scala> List(1,2,3,4,5).product
res19: Int = 120

scala> List(1,2,3,4,5).contains(3)
res20: Boolean = true

scala> List(1,2,3,4,5).max
res21: Int = 5

scala> List(1,2,3,4,5).min
res22: Int = 1

scala> List.range(0,10)
res24: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> List.range(0, 10, 2)
res26: List[Int] = List(0, 2, 4, 6, 8)

scala> List.range(0, 10).take(3)
res28: List[Int] = List(0, 1, 2)

こんな感じ。