School of Haskell
Lenses In Pictures - adit.io
You should know what a functor is before reading this post. Read this to learn about functors. Suppose you want to make a game: Ok, now how would you move this player? moveX (Mario (Point xpos ypos)) val = Mario (Point (xpos + val) ypos) Instead, lenses allow you to write something like this: location.x `over` (+10) $ player1 Or this is the same thing: over (location . x) (+10) player1 Lenses allow you to selectively modify just a part of your data: Much clearer! location is a lens. Fmap You probably know how fmap works, Doctor Watson (read this if you don’t): Well old chap, what if you have nested functors instead? You need to use two fmaps! Now, you probably know how function composition works: What about function composition composition? “If you want to do function composition where a function has two arguments”, says Sherlock, “you need (.).(.)!” “That looks like a startled owl”, exclaims Watson. “Indeed. The type signature for function composition is: (.) :: (b -> c) -> (a -> b) -> (a -> c) Setters
implicit.ly
Functors, Applicatives, And Monads In Pictures - adit.io
updated: May 20, 2013 Here's a simple value: And we know how to apply a function to this value: Simple enough. Lets extend this by saying that any value can be in a context. For now you can think of a context as a box that you can put a value in: Now when you apply a function to this value, you'll get different results depending on the context. data Maybe a = Nothing | Just a In a second we'll see how function application is different when something is a Just a versus a Nothing. Functors When a value is wrapped in a context, you can't apply a normal function to it: This is where fmap comes in. fmap is from the street, fmap is hip to contexts. fmap knows how to apply functions to values that are wrapped in a context. > fmap (+3) (Just 2)Just 5 Bam! Just what is a Functor, really? Functor is a typeclass. A Functor is any data type that defines how fmap applies to it. So we can do this: And fmap magically applies this function, because Maybe is a Functor. > fmap (+3) NothingNothing But in Haskell:
huzaifa barkati
Related:
Related: