Fundamentalist functional programming

Embed Size (px)

Text of Fundamentalist functional programming

  • 1.Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

2. Whatisa XXX Language? XXX = dynamic XXX = imperative XXX = OO 3. WhatisaFunctionalLanguage A language wherefunctionsare first-class citizens 4. EWD1073 5. 6. Easy like Sunday morning 7. DanMeyer WTFwentwrong? 8. Doing IO is a side-effect Console.WriteLine(ha); Console.WriteLine(ha); != Console.WriteLine(ha); varx = Console.ReadLine(); vary = x+x; != vary = Console.ReadLine() + Console.ReadLine() 9. Doing IO is a side-effect let date = DateTime.Now() in (date, date) ( DateTime.Now() , DateTime.Now() ) DateTime.Nowis not a mathematical function 10. If I cannot even do IO FundamentalistFunctional Programming is just academic nonsense 11. Donteat The Yellow Snow! 12. Lazy evaluation and side-effects static boolLessThanThirty( in t x) { Console.Write("{0}? Less than 30;",x);returnx < 30; } static boolMoretHanTwenty( intx) { Console.Write("{0}? More than 20;",x);returnx > 20; } varq0 =fromxinnew[]{ 1, 25, 40, 5, 23 }whereLessThanThirty(x)selectx; varq1 =fromx in q0whereMoreThanTwenty(x)selectx; foreach( varrinq1) Console.WriteLine("[{0}];",r);Order ofside effects? C# 13. 14. let lessThanThirty(x:int)= begin Console.WriteLine ("{0} less than 30?", x); x < 30; end let moreThanTwenty(x:int)= begin Console.WriteLine ("{0} more than 20?", 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 doConsole.WriteLine("> {0},", r) F# Lazy comprehensions 15. let lessThanThirty(x:int)= begin Console.WriteLine ("{0} less than 30?", x); x < 30; end let moreThanTwenty(x:int)= begin Console.WriteLine ("{0} more than 20?", 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("> {0},", r) F# Eager comprehensions 16. varxs =new []{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; IEnumerable q =null ; try { q =fromxinxsselect1/x; } catch { q =newint[]; } foreach ( varzinq)Console.WriteLine(z): // throws here Exceptions 17. 18. Func< string > GetContents =null ; using (varfile =FileSystem.OpenTextFileReader(@"")) { GetContents =() => file.ReadToEnd(); } // surprise! an exceptionConsole.WriteLine(GetContents());Disposing resources 19. varDelayedActions = new List>(); vars = ""; for( vari = 4; i < 7; i++) { DelayedActions.Add( delegate (){returni; }); } for( vark = 0; k < DelayedActions.Count; k++) { s += DelayedActions[k](); } Console.WriteLine(s); Variable Instantiation Prints777 20. varDelayedActions = new List>(); vars = ""; for( vari = 4; i < 7; i++) { varj = i; DelayedActions.Add( delegate (){returnj; }); } for( vark = 0; k < DelayedActions.Count; k++) { s += DelayedActions[k](); } Console.WriteLine(s); Variable Instantiation Prints 456 Prints 666 C# JavaScript 21. varDelayedActions = new List>(); vars = ""; varj; for( vari = 4; i < 7; i++) { j = i; DelayedActions.Add( delegate (){returnj; }); } for( vark = 0; k < DelayedActions.Count; k++) { s += DelayedActions[k](); } Console.WriteLine(s); Variable Instantiation Prints 666 22. varo1 =newobject(); //58225482, for instance Console.WriteLine(o1.GetHashCode()); varo2 =newobject(); // 54267293, for instance Console.WriteLine(o2.GetHashCode()); Debug.Assert(o1 != 02); Object creation 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 24. Page 165: There is a subtle error inon_exit and keep_aliveWrite 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 . 25. 26. GET /person/age GET /clock/seconds few interesting functions are idempotent *not* idempotent *not* pure 27. staticT Identity( thisT me) { if (me.GetType() ==typeof ( Button )) throw newException(); varmini_me = meas string ; if (mini_me !=null )returnhello world; Console.WriteLine(me.ToString()); returnActivator.CreateInstance(); } Dishonest types Notparametric! 28. HOTLanguages To the rescue 29. lessThanThirty x = do { putStrLn $ (show x)++" less than 30?"; return $ x < 30 } moreThanTwenty x = do { putStrLn $ (show x)++" more than 20?" ; return $ x > 20 } q0 = [ x| x 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 42. Can Normal Programmers Understand Fundamentalist Functional Programming? 43. 44. LINQ Monads are the secret sauce behind LINQ IEnumerable SelectMany( IEnumerable src, Func> selector ) Bind 45. Java Throws clause voidFoo( strings)throwsException Explicit Effects 46. Fundamentalist FunctionalProgramming Abstract from evaluation order Makealleffects explicit ==> Compiler can freely rearrange code Runtime can execute in code any order Call-by-name,or call-by-valueversion? 47. Call To Action Buy Graham Huttons book Nudge and nag MS to produce a fundamentalist functional language 48. Buy Bryan John & Dons book Nudge and nagMS to produce afundamentalistfunctional language 49. ThankYou 50. 51.