The secret of Functional Programming revealed!
What we have heard.. <ul><ul><li>In Functional Programming you only use functions! </li></ul></ul><ul><ul><ul><li>So no cl...
What we have heard.. <ul><ul><li>In Functional Programming you have no variables! </li></ul></ul>
<ul><li>Functional Programming </li></ul><ul><li>seems like a couple of steps  </li></ul><ul><li>backwards.. </li></ul>
&quot;Functional Languages are at a  higher level of abstraction  than object-oriented languages. Though they are more dif...
Why FP is incredible! <ul><ul><li>Reduces  bugs </li></ul></ul><ul><ul><ul><li>easier to test  </li></ul></ul></ul><ul><ul...
And the multi-core hype... <ul><ul><li>Functional Programming promises to make concurrent and parallell programming easier...
Language differ in their support of Functional Programming
Key Concept <ul><li>Referential transparency:  </li></ul><ul><li>A  function  always returns the same result, as long as t...
No side effects! <ul><li>The purpose of a function is to do nothing other than return a result. </li></ul><ul><ul><li>No v...
<ul><li>Functional programmers say  side effects  makes your code  dirty ! </li></ul><ul><li>The technical term:  </li></u...
Euler problem #1: <ul><li>Find the sum of all the multiples of 3 or 5 below 1000. </li></ul>If we list all the natural num...
Imperative solution.. <ul><li>static int Solution(int limit) </li></ul><ul><li>{ </li></ul><ul><li>     int sum = 0; </li>...
Recursive solution.. <ul><li>static int Solution(int limit) </li></ul><ul><li>{ </li></ul><ul><li>     if (limit == 0) ret...
Using Linq.. <ul><li>static int Solution(int limit) </li></ul><ul><li>{ </li></ul><ul><li>     return Enumerable.Range(1, ...
FP is insane! <ul><li>&quot;All you can do without side effects is push a button and watch the box get hot for a while.&qu...
So what's the secret? <ul><li>There must be some kind of  magic  that makes you able to implement &quot;normal&quot; progr...
The Philosophy of FP <ul><li>You should break your program into two parts: </li></ul><ul><ul><li>The biggest part:  </li><...
 
Getting started with FP <ul><ul><li>Start using System.Linq for everything: </li></ul></ul><ul><ul><ul><li>Where          ...
Upcoming SlideShare
Loading in …5
×

The secret of Functional Programming revealed!

1,618
-1

Published on

Slides from a ZipTalk i did for the development team at PSWinCom. Code samples in C#.

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

  • Be the first to like this

No Downloads
Views
Total Views
1,618
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

The secret of Functional Programming revealed!

  1. 1. The secret of Functional Programming revealed!
  2. 2. What we have heard.. <ul><ul><li>In Functional Programming you only use functions! </li></ul></ul><ul><ul><ul><li>So no classes or objects..? </li></ul></ul></ul>
  3. 3. What we have heard.. <ul><ul><li>In Functional Programming you have no variables! </li></ul></ul>
  4. 4. <ul><li>Functional Programming </li></ul><ul><li>seems like a couple of steps  </li></ul><ul><li>backwards.. </li></ul>
  5. 5. &quot;Functional Languages are at a higher level of abstraction than object-oriented languages. Though they are more difficult to understand, you can express bigger ideas with less code .&quot; <ul><li>Bruce A. Tate </li></ul><ul><li>Seven Languages in Seven Weeks </li></ul>
  6. 6. Why FP is incredible! <ul><ul><li>Reduces bugs </li></ul></ul><ul><ul><ul><li>easier to test  </li></ul></ul></ul><ul><ul><ul><li>easier to trust </li></ul></ul></ul><ul><ul><li>More compact code </li></ul></ul><ul><ul><ul><li>no variables </li></ul></ul></ul><ul><ul><li>More elegant </li></ul></ul><ul><ul><ul><li>closer to mathematics </li></ul></ul></ul><ul><ul><ul><li>proofs of correctness </li></ul></ul></ul>
  7. 7. And the multi-core hype... <ul><ul><li>Functional Programming promises to make concurrent and parallell programming easier </li></ul></ul>
  8. 8. Language differ in their support of Functional Programming
  9. 9. Key Concept <ul><li>Referential transparency:  </li></ul><ul><li>A function always returns the same result, as long as the same arguments are passed into it. </li></ul>
  10. 10. No side effects! <ul><li>The purpose of a function is to do nothing other than return a result. </li></ul><ul><ul><li>No variables must change (be mutated) </li></ul></ul><ul><ul><li>No visible effects (dialog box, &quot;bing&quot;, etc.) </li></ul></ul><ul><ul><li>No access of external data (files, keyboard, etc.) </li></ul></ul><ul><ul><li>No reference to variables defined elsewhere </li></ul></ul>
  11. 11. <ul><li>Functional programmers say side effects makes your code dirty ! </li></ul><ul><li>The technical term:  </li></ul><ul><li>                 Imperative code </li></ul><ul><li>Imperative code is the opposite of functional code   </li></ul><ul><li>(What about OOP?) </li></ul>
  12. 12. Euler problem #1: <ul><li>Find the sum of all the multiples of 3 or 5 below 1000. </li></ul>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
  13. 13. Imperative solution.. <ul><li>static int Solution(int limit) </li></ul><ul><li>{ </li></ul><ul><li>     int sum = 0; </li></ul><ul><li>     for (int i = 1; i < limit; i++) </li></ul><ul><li>     { </li></ul><ul><li>         if (i % 3 == 0 || i % 5 == 0) </li></ul><ul><li>         { </li></ul><ul><li>             sum += i; </li></ul><ul><li>         } </li></ul><ul><li>     } </li></ul><ul><li>     return sum; </li></ul><ul><li>} </li></ul>
  14. 14. Recursive solution.. <ul><li>static int Solution(int limit) </li></ul><ul><li>{ </li></ul><ul><li>     if (limit == 0) return 0; </li></ul><ul><li>     int c = limit - 1; </li></ul><ul><li>     if (c % 3 == 0 || c % 5 == 0) </li></ul><ul><li>     { </li></ul><ul><li>         return c + Solution(c); </li></ul><ul><li>     }  </li></ul><ul><li>     else </li></ul><ul><li>     { </li></ul><ul><li>         return Solution(c); </li></ul><ul><li>     } </li></ul><ul><li>} </li></ul>
  15. 15. Using Linq.. <ul><li>static int Solution(int limit) </li></ul><ul><li>{ </li></ul><ul><li>     return Enumerable.Range(1, limit - 1) </li></ul><ul><li>         .Where(i =>  </li></ul><ul><li>             i % 3 == 0 || i % 5 == 0) </li></ul><ul><li>         .Sum(); </li></ul><ul><li>} </li></ul>Learn to love starting functions with return!
  16. 16. FP is insane! <ul><li>&quot;All you can do without side effects is push a button and watch the box get hot for a while.&quot; </li></ul><ul><li>Simon Peyton Jones </li></ul>
  17. 17. So what's the secret? <ul><li>There must be some kind of magic that makes you able to implement &quot;normal&quot; programs.. </li></ul><ul><li>Or..? </li></ul>
  18. 18. The Philosophy of FP <ul><li>You should break your program into two parts: </li></ul><ul><ul><li>The biggest part:  </li></ul></ul><ul><ul><ul><li>Completely functional, free of side effects  </li></ul></ul></ul><ul><ul><ul><li>This is the clean part! </li></ul></ul></ul><ul><ul><li>The smaller part:  </li></ul></ul><ul><ul><ul><li>Has all the side effects  </li></ul></ul></ul><ul><ul><ul><li>Interacts with the user / rest of the world  </li></ul></ul></ul><ul><ul><ul><li>This is the dirty part! </li></ul></ul></ul><ul><li>A program like this is written in a functional style . </li></ul><ul><li>(Compare to MVC) </li></ul>
  19. 20. Getting started with FP <ul><ul><li>Start using System.Linq for everything: </li></ul></ul><ul><ul><ul><li>Where           (a.k.a. filter / select ) </li></ul></ul></ul><ul><ul><ul><li>Select           (a.k.a. map / project ) </li></ul></ul></ul><ul><ul><ul><li>Aggregate       (a.k.a. fold / reduce ) </li></ul></ul></ul><ul><ul><ul><li>SelectMany       (a.k.a. mapcat ) </li></ul></ul></ul><ul><ul><li>Practice the clean / dirty separation rules </li></ul></ul><ul><li>  </li></ul><ul><ul><li>If time, learn a FP language, then take what you've learned back </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×