fanscala1 2 scalaの基本

1,595 views
1,441 views

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,595
On SlideShare
0
From Embeds
0
Number of Embeds
260
Actions
Shares
0
Downloads
3
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

fanscala1 2 scalaの基本

  1. 1. 初心者Scala in F@N 第一回初心者
  2. 2. orscala
  3. 3. scala REPL
  4. 4. scala> val i = 1 + 1i: Int = 2scala> i = 1 + 3 イミュータブル(再代入できない)scala> var j = 1 + 1j: Int = 2scala> j = 1 + 3j: Int = 4 ミュータブル(再代入できる) 同じ変数に違う値を再代入するなんてバグ発生の元になるだけだから しない方がいいよという考え方
  5. 5. scala> val i:Int = 1 + 1i: Int = 2scala> val i = 1 + 1i: Int = 2 iの型を省略できる → 1 + 1の結果からiはInt型になるだろうと推論している (型推論)
  6. 6. メソッドと関数scala> def addOne(x: Int): Int = x + 1addOne: (x: Int)Intscala> val addOne = (x: Int) => x + 1addOne: (Int) => Int = <function1>scala> addOne(1)res1: Int = 2
  7. 7. カリー化scala> def multiply(m: Int)(n: Int): Int = m * nscala> multiply(2)(3)scala> val timesTwo = multiply(2) _scala> timesTwo(3)
  8. 8. List定義scala> val list = List(1, 2, 3, 4, 5)list: List[Int] = List(1, 2, 3, 4, 5)
  9. 9. List mapscala> list.map((i: Int) => i * 2)---javaList<Integer> result = new ArrayList<>();for(int i = 0; i < list.size; i++) { result.add(list.get(i) * 2);}
  10. 10. List mapscala> list.map((i: Int) => i * 2)scala> list.map(_ * 2)scala> val f = (i: Int) => i * 2scala> list.map(f)
  11. 11. List foreachscala> list.foreach(println(_))
  12. 12. List filterscala> list.filter(_ % 2 == 0)
  13. 13. List zipscala> val list2 = List(6, 7, 8, 9, 0)scala> list.zip(list2)
  14. 14. List partitionscala> list.partition((_ % 2 == 0))
  15. 15. List foldLeft,foldRightscala> list.foldLeft(0)((m: Int, n: Int) => m + n)scala> list.foldLeft(0)(_ + _)res28: Int = 15scala> list.foldRight(0)((m: Int, n: Int) => m + n)scala> list.fold Right(0)(_ + _)res28: Int = 15
  16. 16. ListScalaDochttp://www.scala-lang.org/api/current/index.html#scala.collection.immutable.ListScala APIhttp://scalajp.github.com/scala-collections-doc-ja/

×