Upgrade to Pro — share decks privately, control downloads, hide ads and more …

はじめての関数型プログラミング教室

 はじめての関数型プログラミング教室

書いて遊ぼうScalaハンズオン

takahiro-hagino

May 26, 2015
Tweet

More Decks by takahiro-hagino

Other Decks in Technology

Transcript

  1. Mac OSΛ͓࢖͍ͷํ XCodeΛΠϯετʔϧ Command Line Developer ToolsΛΠϯετʔϧ $ xcode-select —install

    XCodeͷϥΠηϯεʹಉҙ $ sudo xcodebuild -license HomebrewΠϯετʔϧ $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com /Homebrew/install/master/install)” ScalaΠϯετʔϧ $ brew install scala
  2. Mac OSΛ͓࢖͍ͷํ XCodeΛΠϯετʔϧ Command Line Developer ToolsΛΠϯετʔϧ $ xcode-select —install

    XCodeͷϥΠηϯεʹಉҙ $ sudo xcodebuild -license HomebrewΠϯετʔϧ $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com /Homebrew/install/master/install)” ScalaΠϯετʔϧ $ brew install scala ͜Ε͚ͩ
  3. WindowsΛ͓࢖͍ͷํ • JDKΠϯετʔϧʢJava SEʣ
 http://www.oracle.com/technetwork/jp/java/javase/downloads/index.html • ScalaΠϯετʔϧ
 http://www.scala-lang.org/download/ • ؀ڥม਺Λઃఆ


    ίϯτϩʔϧύωϧʼγεςϜʼγεςϜͷৄࡉઃఆ
 Ϣʔβʔఆٛ؀ڥม਺ʹ৽ن௥Ճʢ໊લ͸PATHʹઃఆʣ
 JDKΛΠϯετʔϧͨ͠ϑΥϧμԼbin·Ͱͷύε;ʢʀͰ۠੾Δʣ
 ScalaΛΠϯετʔϧͨ͠ϑΥϧμԼͷbin·Ͱͷύε
  4. REPL $ scala Welcome to Scala version 2.11.2 (Java HotSpot(TM)

    64-Bit Server VM, Java 1.8.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> println("Hello Scala!") Hello Scala! scala> :quit
  5. REPL $ scala Welcome to Scala version 2.11.2 (Java HotSpot(TM)

    64-Bit Server VM, Java 1.8.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> println("Hello Scala!") Hello Scala! scala> :quit λʔϛφϧ͔Β”scala”ίϚϯυΛ࣮ߦ
  6. REPL $ scala Welcome to Scala version 2.11.2 (Java HotSpot(TM)

    64-Bit Server VM, Java 1.8.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> println("Hello Scala!") Hello Scala! scala> :quit λʔϛφϧ͔Β”scala”ίϚϯυΛ࣮ߦ ऴྃ͢Δͱ͖͸”:quit”΋͘͠͸”:q”
  7. IDE object Main { def main(args: Array[String]) { println("Hello, Scala!")

    def hello(lang:String = "World"): String = "Hello, %s!".format(lang) println(hello("Scala")) } } ͓·͡ͳ͍ ͜Μͳ͔Μ͡ͰॲཧΛॻ͍͍͖ͯ·͢
  8. Scalaͷ͍͍ͱ͜Ζ • ܕਪ࿦ (Type Inference) • ϓϨʔεϗϧμʔ • ౶ҥߏจ (Syntactic

    sugar) • ڧྗͳύλʔϯϚον • ҉໧ͷܕม׵ (Implicit Conversions)
  9. Scala ΫϨυ • var ېࢭ • mutable ېࢭ • while/for

    ϧʔϓ ېࢭ • null ېࢭ • Option.get ېࢭ • return ېࢭ • if ېࢭ (൒෼৑ஊ)
  10. Scala ΫϨυ • var ېࢭ • mutable ېࢭ • while/for

    ϧʔϓ ېࢭ • null ېࢭ • Option.get ېࢭ • return ېࢭ • if ېࢭ (൒෼৑ஊ) ࣾ಺Ͱ͸పఈͯ͠·͢
  11. ม਺એݴ scala> val lang = "scala" lang: String = scala

    scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^
  12. ม਺એݴ scala> val lang = "scala" lang: String = scala

    scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^ ࠶୅ೖෆՄ
  13. ม਺એݴ scala> val lang = "scala" lang: String = scala

    scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^ scala> var lang = "php" lang: String = php ࠶୅ೖෆՄ
  14. ม਺એݴ scala> val lang = "scala" lang: String = scala

    scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^ scala> var lang = "php" lang: String = php scala> lang = "ruby" lang: String = ruby ࠶୅ೖෆՄ
  15. ม਺એݴ scala> val lang = "scala" lang: String = scala

    scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^ scala> var lang = "php" lang: String = php scala> lang = "ruby" lang: String = ruby ࠶୅ೖෆՄ ࠶୅ೖՄ
  16. ܕͷ͋Δม਺એݴ scala> val num0: Int = 1 num0: Int =

    1 scala> val num0: Int = "1" <console>:7: error: type mismatch; found : String("1") required: Int val num0: Int = "1" ^
  17. ܕͷ͋Δม਺એݴ scala> val num0: Int = 1 num0: Int =

    1 scala> val num0: Int = "1" <console>:7: error: type mismatch; found : String("1") required: Int val num0: Int = "1" ^ ܕ͕ҟͳΔ
  18. ܕͷ͋Δม਺એݴ scala> val num0: Int = 1 num0: Int =

    1 scala> val num0: Int = "1" <console>:7: error: type mismatch; found : String("1") required: Int val num0: Int = "1" ^ scala> val num0: String = "1" num0: String = 1 ܕ͕ҟͳΔ
  19. Ϧςϥϧͬͯͳʹʁ scala> 12345 res0: Int = 12345 scala> "BizReach" res1:

    String = BizReach ੔਺Ϧςϥϧ จࣈྻϦςϥϧ
  20. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2>
  21. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2> ؔ਺ϦςϥϧΛ
  22. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2> ؔ਺ϦςϥϧΛ ม਺ʹ୅ೖ
  23. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2> scala> val sum = (x: Int, y: Int) => { | println("x is " + x) | println("y is " + y) | x + y | } sum: (Int, Int) => Int = <function2> ؔ਺ϦςϥϧΛ ม਺ʹ୅ೖ
  24. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2> scala> val sum = (x: Int, y: Int) => { | println("x is " + x) | println("y is " + y) | x + y | } sum: (Int, Int) => Int = <function2> ؔ਺ϦςϥϧΛ ม਺ʹ୅ೖ ෳ਺ߦͷ৔߹
  25. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2> scala> val sum = (x: Int, y: Int) => { | println("x is " + x) | println("y is " + y) | x + y | } sum: (Int, Int) => Int = <function2> scala> sum(2, 3) x is 2 y is 3 res4: Int = 5 ؔ਺ϦςϥϧΛ ม਺ʹ୅ೖ ෳ਺ߦͷ৔߹ ࠷ޙͷߦ͕ධՁ͞ΕΔ
  26. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) جຊతͳListͷ࡞Γํ
  27. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() جຊతͳListͷ࡞Γํ
  28. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ
  29. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ
  30. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ ܕΛએݴͯ͠ม਺ʹ୅ೖ
  31. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) scala> val ls0 = 0 :: ls ls0: List[Int] = List(0, 1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ ܕΛએݴͯ͠ม਺ʹ୅ೖ
  32. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) scala> val ls0 = 0 :: ls ls0: List[Int] = List(0, 1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ ܕΛએݴͯ͠ม਺ʹ୅ೖ Ϧετͷઌ಄ʹ௥Ճ͢Δ஋::ListͰ ৽͍͠ϦετΛͭ͘Γͬ·͢
  33. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) scala> val ls0 = 0 :: ls ls0: List[Int] = List(0, 1, 2, 3) scala> 0 :: 1 :: 2 :: 3 :: Nil res2: List[Int] = List(0, 1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ ܕΛએݴͯ͠ม਺ʹ୅ೖ Ϧετͷઌ಄ʹ௥Ճ͢Δ஋::ListͰ ৽͍͠ϦετΛͭ͘Γͬ·͢
  34. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) scala> val ls0 = 0 :: ls ls0: List[Int] = List(0, 1, 2, 3) scala> 0 :: 1 :: 2 :: 3 :: Nil res2: List[Int] = List(0, 1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ ܕΛએݴͯ͠ม਺ʹ୅ೖ Ϧετͷઌ಄ʹ௥Ճ͢Δ஋::ListͰ ৽͍͠ϦετΛͭ͘Γͬ·͢ ۭͷϦετʹରͯ͠஋Λ௥Ճͨ͠Γ ෳ਺ͷ஋Λ::Ͱ௥Ճग़དྷ·͢
  35. Range scala> 1 to 10 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2,

    3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) 1͔Β10·Ͱͷ੔਺ྻ
  36. Range scala> 1 to 10 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2,

    3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) 1͔Β10·Ͱͷ੔਺ྻ 1͔Β10ະຬͷ੔਺ྻ
  37. Range scala> 1 to 10 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2,

    3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) scala> 1 to 10 by 2 res2: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9) 1͔Β10·Ͱͷ੔਺ྻ 1͔Β10ະຬͷ੔਺ྻ
  38. Range scala> 1 to 10 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2,

    3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) scala> 1 to 10 by 2 res2: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9) 1͔Β10·Ͱͷ੔਺ྻ 1͔Β10ະຬͷ੔਺ྻ 1͔Β10·Ͱެࠩ2ͷ౳ࠩ਺ྻ
  39. Tuple scala> ("JAVA", "Eclipse") res0: (String, String) = (JAVA,Eclipse) scala>

    ("Scala", "IntelliJ") res1: (String, String) = (Scala,IntelliJ)
  40. Tuple scala> ("JAVA", "Eclipse") res0: (String, String) = (JAVA,Eclipse) scala>

    ("Scala", "IntelliJ") res1: (String, String) = (Scala,IntelliJ) ஋ͷηοτͰ͢
  41. Tuple scala> ("JAVA", "Eclipse") res0: (String, String) = (JAVA,Eclipse) scala>

    ("Scala", "IntelliJ") res1: (String, String) = (Scala,IntelliJ) scala> ("Apple", "Orange", "Banana") res2: (String, String, String) = (Apple,Orange,Banana) ஋ͷηοτͰ͢ ෳ਺ͷ஋Λ·ͱΊΔ
  42. Tuple scala> ("JAVA", "Eclipse") res0: (String, String) = (JAVA,Eclipse) scala>

    ("Scala", "IntelliJ") res1: (String, String) = (Scala,IntelliJ) scala> ("Apple", "Orange", "Banana") res2: (String, String, String) = (Apple,Orange,Banana) scala> List(("Apple", "Orange"), ("Strawberry", “Cherry")) res3: List[(String, String)] = List((Apple,Orange), (Strawberry,Cherry)) ஋ͷηοτͰ͢ ෳ਺ͷ஋Λ·ͱΊΔ λϓϧͷϦετΛ࡞Δ
  43. Tuple scala> ("JAVA", "Eclipse") res0: (String, String) = (JAVA,Eclipse) scala>

    ("Scala", "IntelliJ") res1: (String, String) = (Scala,IntelliJ) scala> ("Apple", "Orange", "Banana") res2: (String, String, String) = (Apple,Orange,Banana) scala> List(("Apple", "Orange"), ("Strawberry", “Cherry")) res3: List[(String, String)] = List((Apple,Orange), (Strawberry,Cherry)) ஋ͷηοτͰ͢ ෳ਺ͷ஋Λ·ͱΊΔ λϓϧͷϦετΛ࡞Δ ܕ͸͜ͷΑ͏ʹͳΓ·͢
  44. ύλʔϯϚον object Untitled { def main(args:Array[String]) = { val lang

    = "Scala" lang match { case "Java" => println("Eclipse") case "Scala" => println("IntelliJ") case "PHP" | "Ruby"=> println("CodeRunner") case _ => println("Vim") } } }
  45. ύλʔϯϚον object Untitled { def main(args:Array[String]) = { val lang

    = "Scala" lang match { case "Java" => println("Eclipse") case "Scala" => println("IntelliJ") case "PHP" | "Ruby"=> println("CodeRunner") case _ => println("Vim") } } } “IntelliJ”͕ग़ྗ͞ΕΔ
  46. ύλʔϯϚον object Untitled { def main(args:Array[String]) = { val lang:String

    = "Scala" val ide:String = lang match { case "Java" => "Eclipse" case "Scala" => "IntelliJ" case "PHP" | "Ruby"=> "CodeRunner" case _ => "Vim" } println("Use "+ ide +".") } }
  47. ύλʔϯϚον object Untitled { def main(args:Array[String]) = { val lang:String

    = "Scala" val ide:String = lang match { case "Java" => "Eclipse" case "Scala" => "IntelliJ" case "PHP" | "Ruby"=> "CodeRunner" case _ => "Vim" } println("Use "+ ide +".") } } ஋Λฦ͢͜ͱ΋Ͱ͖·͢
  48. ύλʔϯϚον object Untitled { def main(args:Array[String]) = { val lang:String

    = "Scala" val ide:String = lang match { case "Java" => "Eclipse" case "Scala" => "IntelliJ" case "PHP" | "Ruby"=> "CodeRunner" case _ => "Vim" } println("Use "+ ide +".") } } ஋Λฦ͢͜ͱ΋Ͱ͖·͢ “Use IntelliJ.”͕ग़ྗ͞ΕΔ
  49. foreach scala> val langs = List("Java", "Scala", "PHP") langs: List[String]

    = List(Java, Scala, PHP) scala> langs.foreach ( ) ListΛͭ͘Γ·͢
  50. foreach scala> val langs = List("Java", "Scala", "PHP") langs: List[String]

    = List(Java, Scala, PHP) scala> langs.foreach ( x => println(x) ) ListΛͭ͘Γ·͢
  51. foreach scala> val langs = List("Java", "Scala", "PHP") langs: List[String]

    = List(Java, Scala, PHP) scala> langs.foreach ( x => println(x) ) ListΛͭ͘Γ·͢ ؔ਺ϦςϥϧΛ౉͠·͢
  52. foreach scala> val langs = List("Java", "Scala", "PHP") langs: List[String]

    = List(Java, Scala, PHP) scala> langs.foreach ( x => println(x) ) Java Scala PHP ListΛͭ͘Γ·͢ ؔ਺ϦςϥϧΛ౉͠·͢ Listͷઌ಄͔ΒॱʹɺҾ਺ʹड͚औͬͨؔ਺Λద༻͠·͢
  53. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 1͔Β10·ͰͷListΛͭ͘Δ
  54. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( ) 1͔Β10·ͰͷListΛͭ͘Δ
  55. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( ) 1͔Β10·ͰͷListΛͭ͘Δ
  56. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ
  57. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ Ҿ਺Λ2ͭऔΔؔ਺ϦςϥϧΛ౉͢
  58. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ Ҿ਺Λ2ͭऔΔؔ਺ϦςϥϧΛ౉͢
  59. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ Ҿ਺Λ2ͭऔΔؔ਺ϦςϥϧΛ౉͢
  60. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ Ҿ਺Λ2ͭऔΔؔ਺ϦςϥϧΛ౉͢
  61. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ Ҿ਺Λ2ͭऔΔؔ਺ϦςϥϧΛ౉͢ ઌ಄͔Βॱʹɺܭࢉ݁Ռͱ࣍ͷ஋Λ ౉͞Εͨؔ਺Ϧςϥϧʹద༻ͨ݁͠ՌΛฦ͢
  62. filter scala> val range = List(1, 2, 3, 4, 5,

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

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val odd = range.filter( )
  64. filter scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val odd = range.filter( x => x % 2 == 1 ) BooleanΛฦؔ͢਺ϦςϥϧΛ౉͢
  65. filter scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val odd = range.filter( x => x % 2 == 1 ) scala> val odd = range.filter( x => x % 2 == 1 ) odd: List[Int] = List(1, 3, 5, 7, 9) BooleanΛฦؔ͢਺ϦςϥϧΛ౉͢ ح਺ͷίϨΫγϣϯ͕࡞ΒΕͨ
  66. map scala> val range = List(1, 2, 3, 4, 5,

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

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val twoTimes = range.map( )
  68. map scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val twoTimes = range.map( x => x * 2 )
  69. map scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val twoTimes = range.map( x => x * 2 ) ஋Λड͚औΓɺ৽͍͠஋Λฦؔ͢਺ϦςϥϧΛ౉͢
  70. map scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val twoTimes = range.map( x => x * 2 ) twoTimes: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20) ஋Λड͚औΓɺ৽͍͠஋Λฦؔ͢਺ϦςϥϧΛ౉͢ ΋ͱͷίϨΫγϣϯͷ஋ΛͦΕͧΕ2ഒͨ͠ίϨΫγϣϯ͕ͭ͘ΒΕͨ
  71. 1͔Β100·Ͱͷ੔਺Λग़ྗ # Scala object Print { def main(args: Array[String]) {

    (1 to 100).foreach(println) } } x => println(x) ͷΑ͏ʹؔ਺͕ ஋ΛऔΔ͜ͱ͕໌Β͔ͳͱ͖͸লུՄ
  72. 1͔Β100ͷ੔਺Λͨ͠߹ΘͤΔ # PHP <?php for($i = 1, $sum = 0;

    $i <= 100; $i++) { $sum += $i; } echo($sum); ?>
  73. 1͔Β100ͷ੔਺Λͨ͠߹ΘͤΔ # Sclala object Sum { def main(args: Array[String]) {

    val sum = (1 to 100).reduce((x0, x1) => x0 + x1) println(sum) } }
  74. 1͔Β100ͷ੔਺Λͨ͠߹ΘͤΔ # Sclala object Sum { def main(args: Array[String]) {

    val sum = (1 to 100).reduce((x0, x1) => x0 + x1) println(sum) } } RangeΛ͔ͬ͜ͰғΜͰ࢖͑·͢
  75. 1͔Β100ͷ੔਺Λͨ͠߹ΘͤΔ # Sclala object Sum { def main(args: Array[String]) {

    val sum = (x0: Int, x1: Int) => x0 + x1 val result = (1 to 100).reduce(sum) println(result) } }
  76. 1͔Β100ͷ੔਺Λͨ͠߹ΘͤΔ # Sclala object Sum { def main(args: Array[String]) {

    val sum = (x0: Int, x1: Int) => x0 + x1 val result = (1 to 100).reduce(sum) println(result) } } ؔ਺Λม਺ʹ୅ೖͯ͠ɺ ͜ΕΛ౉͢͜ͱ΋ग़དྷ·͢
  77. 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ # PHP <?php for ($i = 2, $sum =

    0; $i <= 100; $i += 2) { $sum += $i; } print($sum); ?>
  78. 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ # Scala object SumEven { def main(args: Array[String]) {

    val sum = (x0: Int, x1: Int) => x0 + x1 val isEven = (x: Int) => x % 2 == 0 // (1 to 100).filter(x => x % 2 == 0).reduce(_ + _) val result = (1 to 100).filter(isEven).reduce(sum) println(result) } } ίϨΫγϣϯϝιουΛνΣʔϯ͢Δ͜ͱ΋Ͱ͖·͢
  79. 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ # Scala object SumEven { def main(args: Array[String]) {

    val result = (2 to 100 by 2).sum println(result) } } ίϨΫγϣϯʹ͸σϑΥϧτͰ ଟ͘ͷϝιου͕༻ҙ͞Ε͍ͯ·͢
  80. FizzBuzzͷΠϝʔδ • 1, 2, Fizz, 4, Buzz, Fizz, 7, 8,

    Fizz, Buzz,
 11, Fizz, 13, 14, FizzBuzz, …
  81. 1͔Β100·Ͱͷ੔਺ͰFizzBuzz # PHP <?php for ($i = 0; $i <

    100; $i ++) { if (($i % 3) === 0 && ($i % 5) === 0) { print("FizzBuzz".","); } else if($i % 3 === 0) { print("Fizz".","); } else if ($i % 5 === 0) { print("Buzz".","); } else { print($i.","); } } ?>
  82. 1͔Β100·Ͱͷ੔਺ͰFizzBuzz # Scala object FizzBuzz { def main(args:Array[String]) = {

    val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } }
  83. 1͔Β100·Ͱͷ੔਺ͰFizzBuzz # Scala object FizzBuzz { def main(args:Array[String]) = {

    val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } } xΛ3Ͱׂͬͨ͋·Γͱɺ 5Ͱׂͬͨ͋·ΓͷTuple
  84. 1͔Β100·Ͱͷ੔਺ͰFizzBuzz # Scala object FizzBuzz { def main(args:Array[String]) = {

    val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } } xΛ3Ͱׂͬͨ͋·Γͱɺ 5Ͱׂͬͨ͋·ΓͷTuple Tupleͷ஋ͰύλʔϯϚον ͢Δ͜ͱ΋Ͱ͖Δ
  85. 1͔Β100·Ͱͷ੔਺ͰFizzBuzz # Scala object FizzBuzz { def main(args:Array[String]) = {

    val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } } fizzBuzzม਺ʹ౉͞Ε͍ͯΔ ͷ͸ؔ਺Ϧςϥϧ
  86. ֊৐ܭࢉͷྫ • ࣗવ਺ n ͷ֊৐ެࣜ
 n! = n * (n

    - 1) * … * 1 • ͜ΕΛܭࢉ͢Δؔ਺Λఆٛ͢Δ
 ʢ 0! = 1 ʣ
  87. ֊৐ܭࢉͷྫ object Untitled { def main(args:Array[String]) = { def factorial(n:Int):Int

    = if (n < 1) 1 else (n * factorial(n - 1)) println(factorial(1)) // 1 println(factorial(2)) // 2 println(factorial(3)) // 6 println(factorial(4)) // 24 } }
  88. ֊৐ܭࢉͷྫ object Untitled { def main(args:Array[String]) = { def factorial(n:Int):Int

    = if (n < 1) 1 else (n * factorial(n - 1)) println(factorial(1)) // 1 println(factorial(2)) // 2 println(factorial(3)) // 6 println(factorial(4)) // 24 } } ຤ඌ࠶ؼ͡Όͳ͍
  89. ֊৐ܭࢉʢ຤ඌ࠶ؼʣ object Untitled { def main(args:Array[String]) = { def factorial(n:Int,

    acc:Int = 1):Int = if (n < 1) acc else (factorial(n - 1, n * acc)) println(factorial(1)) // 1 println(factorial(2)) // 2 println(factorial(3)) // 6 println(factorial(4)) // 24 } }
  90. ֊৐ܭࢉʢ຤ඌ࠶ؼʣ object Untitled { def main(args:Array[String]) = { def factorial(n:Int,

    acc:Int = 1):Int = if (n < 1) acc else (factorial(n - 1, n * acc)) println(factorial(1)) // 1 println(factorial(2)) // 2 println(factorial(3)) // 6 println(factorial(4)) // 24 } } ΞΩϡϜϨʔλͷಋೖ
  91. ֊৐ܭࢉʢ຤ඌ࠶ؼʣ object Untitled { def main(args:Array[String]) = { def factorial(n:Int,

    acc:Int = 1):Int = if (n < 1) acc else (factorial(n - 1, n * acc)) println(factorial(1)) // 1 println(factorial(2)) // 2 println(factorial(3)) // 6 println(factorial(4)) // 24 } } ΞΩϡϜϨʔλͷಋೖ ͜ΕͰ຤ඌ࠶ؼʹͳͬͨ
  92. ϑΟϘφον਺ྻͷΠϝʔδ • 0, 1, 1, 2, 3, 5, 8, 13,

    21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, …
  93. ϑΟϘφον਺ͷྫ object Fibonacci { def main(args:Array[String]) = { def fib(n:

    Int): Int = if(n < 1) 0 else n match { case 1 => 1 case 2 => 1 case x => fib(x-1) + fib(x-2) } val fibString = (1 to 10).map(fib).mkString(", ") println(fibString) // 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 } } ຤ඌ࠶ؼ͡Όͳ͍
  94. ϑΟϘφον਺ʢ຤ඌ࠶ؼʣ object Fibonacci { def main(args:Array[String]) = { def fibList(lim:

    Int): List[Int] = { def fib0(lim: Int, fls: List[Int]): List[Int] = { if (lim < 1) fls else { fls match { case Nil => fib0(lim-1, 1 :: Nil) case x0 :: Nil => fib0(lim-1, 1 :: fls) case x1 :: x0 :: ls => fib0(lim-1, (x0 + x1) :: x1 :: x0 :: ls) } } } fib0(lim, Nil).reverse } def fib(n: Int): Int = fibList(n).last } } fibListͷ಺෦ʹؔ਺Λ࡞Δ fib0ͷͳ͔Ͱ͸ٯॱͰListΛ͍ͭͬͯ͘ΔͨΊɺ࠷ޙʹ໭͢
  95. ϑΟϘφον਺ྻʢ lazy val ʣ val fibList = (lim: Int) =>

    { lazy val fib0:(Int, List[Int]) => List[Int] = (lim:Int, fls:List[Int]) => { if (lim < 1) fls else { fls match { case Nil => fib0(lim-1, 1 :: Nil) case x0 :: Nil => fib0(lim-1, 1 :: fls) case x1 :: x0 :: ls => fib0(lim-1, (x0 + x1) :: x1 :: x0 :: ls) } } } fib0(lim, Nil).reverse } val fib = (n: Int) => fibList(n).last .lastOption.getOrElse(0)