Slide 1

Slide 1 text

Functional Programming in Scala Reading #1 Sep 12 2013 Katsunori Kanda Software Engineer @potix2 https://github.com/potix2/fpinscala 13೥9݄9೔݄༵೔

Slide 2

Slide 2 text

Chapter1: Keywords • side effect(෭࡞༻) • referential transparency(ࢀরಁաੑ) 13೥9݄9೔݄༵೔

Slide 3

Slide 3 text

1.1 The fundamental premise of functional programming • ͜ͷຊͰ͸ɺ७ਮؔ਺(pure functions) Λ ࢖ͬͯϓϩάϥϜΛॻ͘͜ͱΛؔ਺ܕ ϓϩάϥϛϯά(functional programming) ͱݺͿ • ७ਮؔ਺ => ෭࡞༻Λ࣋ͨͳ͍ؔ਺ 13೥9݄9೔݄༵೔

Slide 4

Slide 4 text

෭࡞༻ͷྫ(1/2) // reassigning a variable var x = 1; x = 2 // modifying a data structure in place map(“foo”) = “bar” // setting a field on an object cup.price = 12 // throwing an exception or halting with an error throw new Exception(“caused error”) 13೥9݄9೔݄༵೔

Slide 5

Slide 5 text

෭࡞༻ͷྫ(2/2) // printing to the console or reading user input println(“hello”) // reading from or writing to a file Source.fromFile(“foo.txt”).mkString // Drawing on the screen def draw() = { // write to screen ... } 13೥9݄9೔݄༵೔

Slide 6

Slide 6 text

1.1.1 A Simple example 13೥9݄9೔݄༵೔

Slide 7

Slide 7 text

1.2 Exactly what is a (pure) function ? 13೥9݄9೔݄༵೔

Slide 8

Slide 8 text

७ਮؔ਺ͱ͸? • ಉ͡Ҿ਺Λ౉ͤ͹ৗʹಉ݁͡Ռ͕ฦΔ • ex1. 2 + 3 (ৗʹ݁Ռ͸5) • ex2. “emacs or vi”.length() (݁Ռ͸11) 13೥9݄9೔݄༵೔

Slide 9

Slide 9 text

΋͏গ͠ܗࣜతʹఆٛ • ७ਮؔ਺ͱ͸ɺຊମ͕ࣜࢀরಁա (referential transparency)Ͱ͋Δؔ਺ 13೥9݄9೔݄༵೔

Slide 10

Slide 10 text

1.2.1 Referential transparency, purity, and the substitution model 13೥9݄9೔݄༵೔

Slide 11

Slide 11 text

७ਮؔ਺ͷྫ java.lang.String.reverse() 13೥9݄9೔݄༵೔

Slide 12

Slide 12 text

७ਮؔ਺Ͱ͸ͳ͍ྫ java.lang.StringBuilder.append() 13೥9݄9೔݄༵೔

Slide 13

Slide 13 text

·ͱΊ • ͜ͷຊͰѻ͏functional programmingͱ͸ • ७ਮؔ਺Λ༻͍ͨϓϩάϥϛϯάͷ͜ͱ • ७ਮؔ਺ͱ͸ຊମ͕ࣜࢀরಁաͰ͋Δؔ਺ 13೥9݄9೔݄༵೔