Your SlideShare is downloading. ×
Thesis
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Thesis

170
views

Published on


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
170
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Paradigm BlendKnowledge Gap for Devs
  • 2. Those interested in Monads have no goodstarting placesCategory Theory
  • 3. Functional abstractions arepatternsOrigin in math
  • 4. What’s a Monad?A Monad is just a Monoid in the categoryof endofunctors
  • 5. Monads are notmetaphors
  • 6. FunctorsFrom the ground up
  • 7. Functors are containersType Constructors - F[A]
  • 8. Functors need to have a Map function mapYou did take PLP with Wallingford,right?
  • 9. def map[A,B](cont: List[A], f: A => B)Apply the function f to each elementHigher Order FunctionsHigher Order Functions
  • 10. Functors need a map methodFunctors are containersLift arity-1 function to a functionthat works on computationalcontext
  • 11. Applicative FunctorsApplicative Functorscurrying functions is fun
  • 12. Lifts an n-arity function to work oncomputational context throughcurrying.Currying?x => (y => x + y)
  • 13. Applicative is just a more generalized Functor
  • 14. Applicative Stylepure(+) <*> pure(3) <*> pure(4)
  • 15. Recap: Functors lift 1-arity Applic lift n-arity
  • 16. Monoida distant cousin
  • 17. Type-class which have values that canbe combinedBinary operation must be associative
  • 18. object StringMonoid { def mAppend(x: String, y: String): String = x + y def mEmpty: String = "" }
  • 19. MonadThe Main Attraction
  • 20. Structure computationsCompose functions of different types that canbe logically combined
  • 21. Monads in a purely functional language(Haskell)Monads in Scala
  • 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. object Monoid { def append(a1: List[A], a2: List[A]) = a1 ::: a2 def empty = Nil }
  • 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. 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. FIN?
  • 27. Monads and ScalaFor - Comprehensions
  • 28. Scala has Monads everywhere!Scala recognizes monadic code.
  • 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. 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