Thesis PPT

720 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
720
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Thesis PPT

  1. 1. Paradigm BlendKnowledge Gap for Devs
  2. 2. Those interested in Monads have no goodstarting placesCategory Theory
  3. 3. Functional abstractions arepatternsOrigin in math
  4. 4. What’s a Monad?A Monad is just a Monoid in the categoryof endofunctors
  5. 5. Monads are notmetaphors
  6. 6. FunctorsFrom the ground up
  7. 7. Functors are containersType Constructors - F[A]
  8. 8. Functors need to have a Map function mapYou did take PLP with Wallingford,right?
  9. 9. def map[A,B](cont: List[A], f: A => B)Apply the function f to each elementHigher Order FunctionsHigher Order Functions
  10. 10. Functors need a map methodFunctors are containersLift arity-1 function to a functionthat works on computationalcontext
  11. 11. Applicative FunctorsApplicative Functorscurrying functions is fun
  12. 12. Lifts an n-arity function to work oncomputational context throughcurrying.Currying?x => (y => x + y)
  13. 13. Applicative is just a more generalized Functor
  14. 14. Applicative Stylepure(+) <*> pure(3) <*> pure(4)
  15. 15. Recap: Functors lift 1-arity Applic lift n-arity
  16. 16. Monoida distant cousin
  17. 17. Type-class which have values that canbe combinedBinary operation must be associative
  18. 18. object StringMonoid { def mAppend(x: String, y: String): String = x + y def mEmpty: String = "" }
  19. 19. MonadThe Main Attraction
  20. 20. Structure computationsCompose functions of different types that canbe logically combined
  21. 21. Monads in a purely functional language(Haskell)Monads in Scala
  22. 22. case class Identity[A](value: A) { def map[B](f: A => B) = Identity(f(value)) def flatMap[B](f: A => Identity[B]) (value) def unit(a: A) = Identity(a)}
  23. 23. object Monoid { def append(a1: List[A], a2: List[A]) = a1 ::: a2 def empty = Nil }
  24. 24. case class Writer[Log, A](log: Log, value: A) { def map[B](f: A => B) = Writer(log, f(value)) def flatMap[B](f: A => Writer[Log, B])(m: Monoid[Log]) = { val x: Writer[Log,B] = f(value) Writer(m.append(log, x.log), x.value) } def unit[Log, A](value: A)(m: Monoid[Log]) = Writer(m.empty, value)}
  25. 25. def main(args: Array[String]) { val start = 3 val finalWriter = for(a <- addOne(start); b <- intString(a); c <- lengthGreaterThan5(b); d <- noLog(oneOrZero(c)); e <- squareOf(d) ) yield e }“Adding one to 3.Changing 4 to a String.Checking if length is greater than 5.Squaring 1.”
  26. 26. FIN?
  27. 27. Monads and ScalaFor - Comprehensions
  28. 28. Scala has Monads everywhere!Scala recognizes monadic code.
  29. 29. val first = List(1)val second = List(4)val third = List(8)for { first flatMap { x <- first x => second flatMap { y <- second y=> third map { z <- third z => x + y + z} )yield ( x + y + z) ) )
  30. 30. case class Transaction(memberInfo: Option[MemberInfo], id: String)case class MemberInfo(privateInfo: Option[PrivateMember], birthdate:String)case class PrivateInfo(socialSecurityNumber: String)val ssNumber = “389-39-2983”val priv = PrivateInfo(ssNumber)val member = MemberInfo(priv, “10-20-87”)val optionalTransaction = Transaction(member, “28948”)for { transaction <- optionalTransaction memberInfo <- transaction.memberInfo privInformation <- memberInfo.privateInfo}yield privInformation.socialSecurityNumber

×