Upcoming SlideShare
×

# Fundamentalist functional programming

422

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

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

• Be the first to like this

Views
Total Views
422
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
15
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Fundamentalist functional programming

1. 1. Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
2. 2. What is a XXX Language? XXX = dynamic XXX = imperative XXX = OO
3. 3. What is a Functional Language A language where functions are first-class citizens
4. 4. EWD 1073
5. 6. Easy like Sunday morning
6. 7. Dan Meyer WTF went wrong?
7. 8. Doing IO is a side-effect Console.WriteLine(“ha”); Console.WriteLine(“ha”); != Console.WriteLine(“ha”); var x = Console.ReadLine(); var y = x+x; != var y = Console.ReadLine() + Console.ReadLine()
8. 9. Doing IO is a side-effect let date = DateTime.Now() in (date, date)  ( DateTime.Now() , DateTime.Now() ) DateTime.Now is not a mathematical function
9. 10. If I cannot even do IO Fundamentalist Functional Programming is just academic nonsense
10. 11. Don’t eat The Yellow Snow!
11. 12. Lazy evaluation and side-effects static bool LessThanThirty( in t x) { Console.Write(&quot;{0}? Less than 30;&quot;,x); return x < 30; } static bool MoretHanTwenty( int x) { Console.Write(&quot;{0}? More than 20;&quot;,x); return x > 20; } var q0 = from x in new[]{ 1, 25, 40, 5, 23 } where LessThanThirty(x) select x; var q1 = from x in q0 where MoreThanTwenty(x) select x; foreach ( var r in q1) Console.WriteLine(&quot;[{0}];&quot;,r); Order of side effects? C#
12. 14. let lessThanThirty(x:int)= begin Console.WriteLine (&quot;{0} less than 30?&quot;, x); x < 30; end let moreThanTwenty(x:int)= begin Console.WriteLine (&quot;{0} more than 20?&quot;, x); x > 20; end let q0 = seq { for x in [1; 25; 40; 5; 23] do if lessThanThirty x then yield x } let q1 = seq { for x in q0 do if moreThanTwenty x then yield x } for r in q1 do Console.WriteLine(&quot;> {0},&quot;, r) F# Lazy comprehensions
13. 15. let lessThanThirty(x:int)= begin Console.WriteLine (&quot;{0} less than 30?&quot;, x); x < 30; end let moreThanTwenty(x:int)= begin Console.WriteLine (&quot;{0} more than 20?&quot;, x); x > 20; end let q0 = [ for x in [1; 25; 40; 5; 23] do if lessThanThirty x then yield x ] let q1 = [ for x in q0 do if moreThanTwenty x then yield x ] for r in q1 do Console.WriteLine(&quot;> {0},&quot;, r) F# Eager comprehensions
14. 16. var xs = new []{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; IEnumerable<int> q = null ; try { q = from x in xs select 1/x; } catch { q = new int[]; } foreach ( var z in q) Console.WriteLine(z): // throws here Exceptions
15. 18. Func< string > GetContents = null ; using ( var file = FileSystem.OpenTextFileReader(@&quot;…&quot;)) { GetContents = () => file.ReadToEnd(); } // surprise! an exception Console.WriteLine(GetContents()); Disposing resources
16. 19. var DelayedActions = new List<Func<int>>(); var s = &quot;&quot;; for ( var i = 4; i < 7; i++) { DelayedActions.Add( delegate (){ return i; }); } for ( var k = 0; k < DelayedActions.Count; k++) { s += DelayedActions[k](); } Console.WriteLine(s); Variable Instantiation Prints 777
17. 20. var DelayedActions = new List<Func<int>>(); var s = &quot;&quot;; for ( var i = 4; i < 7; i++) { var j = i; DelayedActions.Add( delegate (){ return j; }); } for ( var k = 0; k < DelayedActions.Count; k++) { s += DelayedActions[k](); } Console.WriteLine(s); Variable Instantiation Prints 456 Prints 666 C# JavaScript
18. 21. var DelayedActions = new List<Func<int>>(); var s = &quot;&quot;; var j; for ( var i = 4; i < 7; i++) { j = i; DelayedActions.Add( delegate (){ return j; }); } for ( var k = 0; k < DelayedActions.Count; k++) { s += DelayedActions[k](); } Console.WriteLine(s); Variable Instantiation Prints 666
19. 22. var o1 = new object(); //58225482, for instance Console.WriteLine(o1.GetHashCode()); var o2 = new object(); // 54267293, for instance Console.WriteLine(o2.GetHashCode()); Debug.Assert(o1 != 02); Object creation
20. 23. new_cell(X) -> spawn(fun() -> cell(X) end). cell(Value) -> receive {set, NewValue} -> cell(NewValue); {get, Pid} -> Pid!{return, Value}, cell(Value); {dispose} -> {} end. set_cell(Cell, NewValue) -> Cell!{set, NewValue}. get_cell(Cell) -> Cell!{get, self()}, receive {return, Value} -> Value end. dispose_cell(Cell) -> Cell!{dispose}. Concurrency
21. 24. Page 165: There is a subtle error in on_exit and keep_alive … Write your code such that race conditions cannot happen. … Fortunately, the OTP libraries have code for building servers, supervisor trees, and so on. These libraries have been well tested and should not suffer from any race conditions .
22. 26. GET …/person/age… GET …/clock/seconds… few interesting functions are idempotent *not* idempotent *not* pure
23. 27. static T Identity<T>( this T me) { if (me.GetType() == typeof ( Button )) throw new Exception(); var mini_me = me as string ; if (mini_me != null ) return “hello world”; Console.WriteLine(me.ToString()); return Activator.CreateInstance<T>(); } “ Dishonest” types Not parametric!
24. 28. HOT Languages To the rescue
25. 29. lessThanThirty x = do { putStrLn \$ (show x)++&quot; less than 30?&quot; ; return \$ x < 30 } moreThanTwenty x = do { putStrLn \$ (show x)++&quot; more than 20?&quot; ; return \$ x > 20 } q0 = [ x | x <- [1, 25, 50, 5, 23 ] , lessThanThirty x ] q1 = [ x | x <- q0 , moreThanTwenty 20 ] Couldn't match expected type `Bool' against inferred type `IO Bool' In a stmt of a list comprehension: lessThanThirty x In the expression: [x | x <- [1, 25, 50, ....], lessThanThirty x] In the definition of `q0': q0 = [x | x <- [1, 25, ....], lessThanThirty x Haskell
26. 30. unsafePerformIO :: IO a -> a unsafeCast :: a -> b unsafeCast x = unsafePerformIO \$ do{ writeIORef castref x ; readIORef castref } castref = unsafePerformIO \$ do{ newIORef undefined } Forget about effects? Forget it!
27. 31. Cover up effects
28. 32. Remove all effects (cannot make language more powerful by removing features)
29. 33. Don’t fight effects Embrace and extend
30. 34. Mens sana in corpore sano
31. 35. Mistake DateTime.Now()  DateTime Type error !!!!!!!!!!!!!!!
32. 36. Values vs Computation How to turn something mutable into something immutable? Time changes, the clock does not
33. 37. … 9:15 AM 9:16 AM 9:17 AM 9:19 AM 9:29 AM 9:21 AM 9:22 AM 9:23 AM 9:24 AM 9:25 AM 9:26 AM 9:27 AM 9:28 AM 9:29 AM 9:30 AM … It’s just a collection All problems in computer science can be solved by another level of indirection D. Wheeler
34. 38. Mathematics is always right
35. 39. Multithreading  STM<T> Destructive updates  IO<T> Reflection  Mirror<T> Exceptions  Throws<T> Only one way to be pure, But many ways to be impure
36. 40. The “M” word Monads M<A> A computation that produces a value of type A with side-effects described by M M  IO Exception Animation Collection
37. 41. IO data IO a putChar :: Char -> IO () getChar :: IO Char newIORef :: a -> IO (IORef a) readIORef :: IORef a -> IO a writeIORef :: IORef a -> a -> IO () forkIO :: IO a -> IO ThreadID Side-effecting computation that yields a value of type a
38. 42. Can Normal Programmers Understand Fundamentalist Functional Programming?
39. 44. LINQ Monads are the secret sauce behind LINQ IEnumerable <S> SelectMany<T,S>( IEnumerable<T > src, Func<T, IEnumerable<S >> selector ) Bind
40. 45. Java Throws clause void Foo( string s) throws Exception Explicit Effects
41. 46. Fundamentalist Functional Programming Abstract from evaluation order Make all effects explicit ==> Compiler can freely rearrange code Runtime can execute in code any order Call-by-name, or call-by-value version?
42. 47. Call To Action Buy Graham Hutton’s book Nudge and nag MS to produce a fundamentalist functional language
43. 48. Buy Bryan John & Don’s book Nudge and nag MS to produce a fundamentalist functional language
44. 49. Thank You
1. #### Gostou de algum slide específico?

Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.