Successfully reported this slideshow.
Upcoming SlideShare
×

of

Upcoming SlideShare
オンプレミスから AWS への劇的ビフォーアフター
Next

8

Share

# Scalaエンジニアのためのモナド入門

2016年7月10日のScala関西勉強会の発表資料の修正版です

See all

See all

### Scalaエンジニアのためのモナド入門

1. 1. Scala 1 3 Scala 2016 7 10 erin
2. 2. • • Scalaz • • Haskell 2
3. 3. • • Scala • 3
4. 4. • → Haskell • Scala → List Option map flatMap for • → 4
5. 5. • • 5
6. 6. • • 6
7. 7. • → IO • → 7
9. 9. • • 9
11. 11. • • IO 11
12. 12. • • • • 12
13. 13. 13
14. 14. • → • 14
15. 15. 15
16. 16. • • 16
17. 17. • List Option IO State • → 17
18. 18. • 18
19. 19. • → • → A => M[B] • → M[A] >>= (A => M[B]) → x >>= f = ??? → ??? >>= → flatMap 19 Int=>List[String] List[String] >>= (String=>List[Int])
20. 20. • Option → None None Some(4).flatMap(a=>(Some(3).flatMap(b=>Some(a*b)))) // Some(12) Some(4).flatMap(a=>((None:Option[Int]).flatMap(b=>Some(a*b)))) // None • List → List(1,2,3).flatMap(a=>(List(4,5,6).flatMap(b=>List(a*b)))) // List(4, 5, 6, 8, 10, 12, 12, 15, 18) • State → 20 flatMap
21. 21. val om = for { a <- Some(4) b <- None:Option[Int] } yield (a*b) // None val lm = for { a <- List(1,2,3) b <- List(4,5,6) } yield (a*b) // List(4, 5, 6, 8, 10, 12, 12, 15, 18) 21 for
22. 22. (A => M[B]) >>= (B => M[C]) >>= (C => M[D]) → A => M[D] → val lm = (x:Int) => for { a <- List(1,2,3).map(_*x) b <- List(4,5,6).map(_*a) } yield b lm(2) // List(8, 10, 12, 16, 20, 24, 24, 30, 36) 22 List
23. 23. • • 23
24. 24. 24
25. 25. • • IO Reader Writer • 25 State
26. 26. • A => S => (B,S) B => S => (C,S) C => S => (D,S) • A => M[B] B => M[C] C => M[D] → (A => M[B]) >>= (B => M[C]) >>= (C => M[D]) → State[S,A] >>= (A => State[S,B]) → flatMap 26
27. 27. case class State[S,A](runState: S => (A,S)) { def flatMap[B]:(A=>State[S,B])=>State[S,B] = f => State(s => { val (a,s1) = this.runState(s) f(a).runState(s1) }) def map[B]:(A=>B)=>State[S,B] = f => flatMap(a=>State.unit(f(a))) } case object State { def unit[S,A]:A=>State[S,A] = a => State(s => (a,s)) } 27 flatMap
28. 28. • 28 State
29. 29. val push:Int=>State[List[Int],Unit] = a => State(s => ((), a::s)) val pop:State[List[Int],Int] = State(s => (s.head, s.tail)) val calc:(Int=>Int=>Int)=>State[List[Int],Unit] = f => { for { a <- pop b <- pop _ <- push (f(a)(b)) } yield () } val plus:Int=>Int=>Int = a => b => b+a val minus:Int=>Int=>Int = a => b => b-a val mult:Int=>Int=>Int = a => b => b*a val div:Int=>Int=>Int = a => b => b%a 29
30. 30. // 2 3 + 4 1 - * = (2+3)*(4-1) val dc = for { _ <- push (2) _ <- push (3) _ <- calc (plus) _ <- push (4) _ <- push (1) _ <- calc (minus) _ <- calc (mult) } yield () dc.runState(List.empty) 30
31. 31. • • • State 31 Parser
32. 32. case class Parser[S,A](runParser: S => Option[(A,S)]) { def flatMap[B]:(A=>Parser[S,B])=>Parser[S,B] = f => Parser(s => { val o = this.runParser(s) o.flatMap{ case (a,s1) => f(a).runParser(s1) } }) def map[B]:(A=>B)=>Parser[S,B] = f => flatMap(a=>Parser.unit(f(a))) } case object Parser { def unit[S,A]:A=>Parser[S,A] = a => Parser(s => Some((a,s))) } 32 flatMap
33. 33. • 33 Parser
34. 34. val item:Parser[String,Char] = { Parser(s => if (s.length > 0) { Some((s.head, s.tail)) } else { None }) } val satisfy:(Char=>Boolean)=>Parser[String,Char] = f => { for { c <- item n <- Parser(s => if (f(c)) { Some((c,s)) } else { None }):Parser[String,Char] } yield n } val char:Char=>Parser[String,Char] = c => satisfy((x:Char)=>x==c) val digit:Parser[String,Char] = satisfy((x:Char)=>x.isDigit) val letter:Parser[String,Char] = satisfy((x:Char)=>x.isLetter) 34
35. 35. val p = for { a <- letter b <- char(a) c <- digit d <- digit e <- digit f <- digit } yield List(a,b,c,d,e,f).mkString p.runParser(“AA0123") // Some(“AA0123”,) p.runParser(“AB0123") // None p.runParser("A90123") // None p.runParser("990123") // None 35
36. 36. • • 36
37. 37. • flatMap • → for • → • → 37
38. 38. • Scala • 38
39. 39. 39
• #### ssuser851db7

Apr. 16, 2019
• #### historyhistoria

Nov. 13, 2018

Oct. 3, 2018

Sep. 4, 2016
• #### 7daysbrain

Aug. 30, 2016

Aug. 21, 2016

Aug. 3, 2016
• #### ErikaAndo

Jul. 24, 2016

2016年7月10日のScala関西勉強会の発表資料の修正版です

Total views

3,228

On Slideshare

0

From embeds

0

Number of embeds

813

11

Shares

0