Getting the MVVM kicked out of your F#'n Monads  Richard Minerich F# MVP of the Year Senior Researcher, Bayard Rock @Ricka...
Time Warp back to 6 th  Grade <ul><li>1 + (2 + 3) = 6 and (1 + 2) + 3 = 6 </li></ul><ul><li>and </li></ul><ul><li>1 * (2 *...
What about functions? <ul><li>let addOne x = x + 1 </li></ul><ul><li>let addTwo x = x + 2 </li></ul><ul><li>let addThree x...
>> is like add for functions <ul><li>let addOne x = x + 1 </li></ul><ul><li>let addTwo x = x + 2 </li></ul><ul><li>let add...
5 + 0 = 5 and 3 * 1  = 3 <ul><li>What would be the same thing for a function? </li></ul>
5 + 0 = 5 and 3 * 1  = 3 <ul><li>What would be the same thing for a function? </li></ul><ul><li>let id f = f </li></ul><ul...
functions are like numbers <ul><li>Just like (2 + 3) + 4 = 2 + (3 + 4) </li></ul><ul><li>let addSix = (addOne >> addTwo) >...
(2 / 3) / 4 isn’t 2 / (3 / 4) but,  <ul><li>let divByTwo x = x / 2 </li></ul><ul><li>let divByThree x = x / 3 </li></ul><u...
(1 - 2) - 3 isn’t 1 - (2 - 3) but, <ul><li>let minusOne x = x - 1 </li></ul><ul><li>let minusTwo x = x - 2 </li></ul><ul><...
(1 - 2) - 3 isn’t 1 - (2 - 3) cont. <ul><li>let minusSix = minusTwo >> minusThree >> minusOne </li></ul><ul><li>Works grea...
School’s out! <ul><li>First class functions are in! </li></ul>
One Style of Abstraction <ul><li>let ten = [1; 2; 3; 4] |> List.fold (fun x y -> x + y) 0 </li></ul><ul><li>let ten = [1; ...
It works with functions too! <ul><li>let ten =  </li></ul><ul><li>[addOne; addTwo; addThree; addFour]  </li></ul><ul><li>|...
Computation Expressions <ul><li>Will Blow Your Mind </li></ul>
Computation Expressions! <ul><li>-> Each line is a lambda. </li></ul><ul><li>-> In normal class methods you dispatch on ty...
What can you manipulate? <ul><li>Binding, Yielding and Returning </li></ul><ul><li>For and While </li></ul><ul><li>Use, Tr...
Let’s take a look at a simple CE <ul><li>type MultBuilder () =  </li></ul><ul><li>member t.Combine (x,y) = x * y </li></ul...
It goes much deeper. <ul><li>Return – Stuff something in a container </li></ul><ul><li>Bind – Pull it out  </li></ul><ul><...
Option <ul><li>type Option<'a> = | Some of 'a | None </li></ul><ul><li>Most easily used as: </li></ul><ul><li>match x with...
Maybe <ul><li>Maybe allows us to short circuit when something goes wrong, instead of matching again and again. </li></ul>
“ F# 2.0 is a book that assumes you are a professional,  and as such, does not waste your time with a lot of fluff.”
Check out F#! <ul><li>Blog:  RichardMinerich.com </li></ul><ul><li>Twitter:  @Rickasaurus </li></ul><ul><li>F# Community N...
We thank the following companies for their gracious sponsorship Platinum Sponsor Gold Sponsors Silver Sponsors
Upcoming SlideShare
Loading in …5
×

Getting the MVVM Kicked Out of Your F#'n Monads

4,540 views
4,385 views

Published on

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

No Downloads
Views
Total views
4,540
On SlideShare
0
From Embeds
0
Number of Embeds
2,442
Actions
Shares
0
Downloads
9
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Where we put the parens doesn’t matter, the result will be equal
  • We can combine the functions by calling them one after another, but this isn’t really an operation, just passing
  • This ends up having same effect as stacking functions inside calls to each other, but is much more flexible. Now, we can do a lot of things with functions that would could only do with numbers before.
  • This is called the identity F# comes with the id function built in
  • This is called the identity F# comes with the id function built in
  • This is associativity all stateless functions share this property because execution is delayed
  • The &gt;&gt; operator demands associtivity and so everything must be defined in a way that enforces it
  • Because minusTwo, minusOne and minusThree are really the same as addNegativeOne, addNegativeTwo and addNegativeThree The order of calculation is in the order in which things are appended.
  • For this same reason, our addition functions have exactly the same properties as addition itself. You can put parans anywhere you want but you can’t reorder without breaking the function
  • But first we need to revisit some very basic math. Trust me, it will be worth it.
  • Computation expressions let us define the relationships between things in a specific context. Let’s take this apart.
  • But don’t worry, we’ll take it slow
  • Let’s take small steps first. Look at this code.
  • With this pattern, everything is designed in terms of merging containers, putting things in the container or pulling things out
  • With a maybe monad, we can pretend like the maybe isn’t even there
  • Getting the MVVM Kicked Out of Your F#'n Monads

    1. 1. Getting the MVVM kicked out of your F#'n Monads Richard Minerich F# MVP of the Year Senior Researcher, Bayard Rock @Rickasaurus / [email_address] Fill this space with whatever you want (graphic, logo, whatever)
    2. 2. Time Warp back to 6 th Grade <ul><li>1 + (2 + 3) = 6 and (1 + 2) + 3 = 6 </li></ul><ul><li>and </li></ul><ul><li>1 * (2 * 3) = (1 * 2) * 3 </li></ul>
    3. 3. What about functions? <ul><li>let addOne x = x + 1 </li></ul><ul><li>let addTwo x = x + 2 </li></ul><ul><li>let addThree x = x + 3 </li></ul><ul><li>All are int -> int </li></ul><ul><li>let addSix x = addOne(addTwo(addThree(x))) </li></ul>
    4. 4. >> is like add for functions <ul><li>let addOne x = x + 1 </li></ul><ul><li>let addTwo x = x + 2 </li></ul><ul><li>let addThree x = x + 3 </li></ul><ul><li>All are int -> int </li></ul><ul><li>let addSix = addOne >> addTwo >> addThree </li></ul>
    5. 5. 5 + 0 = 5 and 3 * 1 = 3 <ul><li>What would be the same thing for a function? </li></ul>
    6. 6. 5 + 0 = 5 and 3 * 1 = 3 <ul><li>What would be the same thing for a function? </li></ul><ul><li>let id f = f </li></ul><ul><li>So, </li></ul><ul><li>addFive >> id = addFive </li></ul>
    7. 7. functions are like numbers <ul><li>Just like (2 + 3) + 4 = 2 + (3 + 4) </li></ul><ul><li>let addSix = (addOne >> addTwo) >> addThree </li></ul><ul><li>is the same as </li></ul><ul><li>let addSix = addOne >> (addTwo >> addThree) </li></ul><ul><li>Just like 6 = 0 + 6 </li></ul><ul><li>addSix = id >> addSix </li></ul>
    8. 8. (2 / 3) / 4 isn’t 2 / (3 / 4) but, <ul><li>let divByTwo x = x / 2 </li></ul><ul><li>let divByThree x = x / 3 </li></ul><ul><li>let divByFour x = x / 4 </li></ul><ul><li>let divByTwentyFour = (divByTwo >> divByThree) >> divByFour </li></ul><ul><li>is the same as </li></ul><ul><li>let divByTwentyFour = divByTwo >> (divByThree >> divByFour) </li></ul><ul><li>Why? </li></ul>
    9. 9. (1 - 2) - 3 isn’t 1 - (2 - 3) but, <ul><li>let minusOne x = x - 1 </li></ul><ul><li>let minusTwo x = x - 2 </li></ul><ul><li>let minusThree x = x - 3 </li></ul><ul><li>let minusSix = (minusOne >> minusTwo) >> minusThree </li></ul><ul><li>is the same as </li></ul><ul><li>let minusSix = minusOne >> (minusTwo >> minusThree) </li></ul><ul><li>Why? </li></ul>
    10. 10. (1 - 2) - 3 isn’t 1 - (2 - 3) cont. <ul><li>let minusSix = minusTwo >> minusThree >> minusOne </li></ul><ul><li>Works great. But consider.. </li></ul><ul><li>let minusSix = </li></ul><ul><li>timesTwo >> divTwo >> minusThree >> minusThree </li></ul><ul><li>let ohNo = </li></ul><ul><li> minusThree >> divTwo >> minusThree >> timesTwo </li></ul>
    11. 11. School’s out! <ul><li>First class functions are in! </li></ul>
    12. 12. One Style of Abstraction <ul><li>let ten = [1; 2; 3; 4] |> List.fold (fun x y -> x + y) 0 </li></ul><ul><li>let ten = [1; 2; 3; 4] |> List.reduce (fun x y -> x + y) </li></ul><ul><li>let ten = [1; 2; 3; 4] |> List.reduce (+) </li></ul><ul><li>let ten = [1; 2; 3; 4] |> List.sum </li></ul><ul><li>Fold – You provide the identity </li></ul><ul><li>Reduce – No identity needed </li></ul><ul><li>Reduce – Actually, just the operator is good </li></ul><ul><li>Sum – Complete package, you provide nothing </li></ul>
    13. 13. It works with functions too! <ul><li>let ten = </li></ul><ul><li>[addOne; addTwo; addThree; addFour] </li></ul><ul><li>|> List.fold (fun x f -> f x) 0 </li></ul><ul><li>let ten = </li></ul><ul><li>[addOne; addTwo; addThree; addFour] </li></ul><ul><li>|> List.reduce (>>) </li></ul><ul><li><| 0 </li></ul><ul><li>But how do we abstract ideas like these over the inside of functions? </li></ul>
    14. 14. Computation Expressions <ul><li>Will Blow Your Mind </li></ul>
    15. 15. Computation Expressions! <ul><li>-> Each line is a lambda. </li></ul><ul><li>-> In normal class methods you dispatch on type, with computation expressions you dispatch on language constructs. </li></ul>
    16. 16. What can you manipulate? <ul><li>Binding, Yielding and Returning </li></ul><ul><li>For and While </li></ul><ul><li>Use, Try-With and Try-Finally </li></ul><ul><li>Per-function </li></ul><ul><li>The Entire Computation </li></ul><ul><li>The computation can be manipulated at all of these levels. </li></ul>
    17. 17. Let’s take a look at a simple CE <ul><li>type MultBuilder () = </li></ul><ul><li>member t.Combine (x,y) = x * y </li></ul><ul><li>member t.Yield (x) = x </li></ul><ul><li>member x.Delay(f) = f() </li></ul><ul><li>let mult = new MultBuilder() </li></ul><ul><li>mult {yield 2; yield 3} </li></ul>
    18. 18. It goes much deeper. <ul><li>Return – Stuff something in a container </li></ul><ul><li>Bind – Pull it out </li></ul><ul><li>(or decide what to do it’s empty) </li></ul><ul><li>Computation expressions allow us to do this behind the scenes in an elegant way. </li></ul>
    19. 19. Option <ul><li>type Option<'a> = | Some of 'a | None </li></ul><ul><li>Most easily used as: </li></ul><ul><li>match x with </li></ul><ul><li>| Some x -> x </li></ul><ul><li>| None -> raise “Oh god, what now!” </li></ul><ul><li>But we can do better. </li></ul>
    20. 20. Maybe <ul><li>Maybe allows us to short circuit when something goes wrong, instead of matching again and again. </li></ul>
    21. 21. “ F# 2.0 is a book that assumes you are a professional, and as such, does not waste your time with a lot of fluff.”
    22. 22. Check out F#! <ul><li>Blog: RichardMinerich.com </li></ul><ul><li>Twitter: @Rickasaurus </li></ul><ul><li>F# Community News: FSharpCentral.com </li></ul><ul><li>And the NYC F# User Group: </li></ul><ul><li>http://www.meetup.com/nyc-fsharp </li></ul><ul><li>We meet monthly right here! </li></ul>
    23. 23. We thank the following companies for their gracious sponsorship Platinum Sponsor Gold Sponsors Silver Sponsors

    ×