Successfully reported this slideshow.
Upcoming SlideShare
×

4,780 views

Published on

Published in: Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

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>
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>
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>