Скачать презентацию Disclaimer The opinions expressed herein are my own Скачать презентацию Disclaimer The opinions expressed herein are my own

93a2b1eb07cf19a875296860da6d8a84.ppt

  • Количество слайдов: 51

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

What is a XXX Language? XXX = dynamic XXX = imperative XXX = OO What is a XXX Language? XXX = dynamic XXX = imperative XXX = OO

What is a Functional Language A language where functions are first-class citizens What is a Functional Language A language where functions are first-class citizens

EWD 1073 EWD 1073

Easy like Sunday morning Easy like Sunday morning

Dan Meyer WTF went wrong? Dan Meyer WTF went wrong?

Doing IO is a side-effect Console. Write. Line(“ha”); != Console. Write. Line(“ha”); var x Doing IO is a side-effect Console. Write. Line(“ha”); != Console. Write. Line(“ha”); var x = Console. Read. Line(); var y = x+x; != var y = Console. Read. Line() + Console. Read. Line()

Doing IO is a side-effect let date = Date. Time. Now() in (date, date) Doing IO is a side-effect let date = Date. Time. Now() in (date, date) ( Date. Time. Now() , Date. Time. Now() ) Date. Time. Now is not a mathematical function

If I cannot even do IO Fundamentalist Functional Programming is just academic nonsense If I cannot even do IO Fundamentalist Functional Programming is just academic nonsense

Don’t eat The Yellow Snow! Don’t eat The Yellow Snow!

C# Lazy evaluation and side-effects static bool Less. Than. Thirty(int x) { Console. Write( C# Lazy evaluation and side-effects static bool Less. Than. Thirty(int x) { Console. Write("{0}? Less than 30; ", x); return x < 30; } static bool Moret. Han. Twenty(int x) { Console. Write("{0}? More than 20; ", x); return x > 20; } var q 0 = from x in new[]{ 1, 25, 40, 5, 23 } where Less. Than. Thirty(x) select x; var q 1 = from x in q 0 where More. Than. Twenty(x) select x; Order of side effects? foreach (var r in q 1) Console. Write. Line("[{0}]; ", r);

let less. Than. Thirty(x: int)= begin Console. Write. Line ( let less. Than. Thirty(x: int)= begin Console. Write. Line ("{0} less than 30? ", x); x < 30; end let more. Than. Twenty(x: int)= begin Console. Write. Line ("{0} more than 20? ", x); x > 20; end let q 0 = seq { for x in [1; 25; 40; 5; 23] do if less. Than. Thirty x then yield x } let q 1 = seq { for x in q 0 do if more. Than. Twenty x then yield x } for r in q 1 do Console. Write. Line("> {0}, ", r) F# Lazy comprehensions

let less. Than. Thirty(x: int)= begin Console. Write. Line ( let less. Than. Thirty(x: int)= begin Console. Write. Line ("{0} less than 30? ", x); x < 30; end let more. Than. Twenty(x: int)= begin Console. Write. Line ("{0} more than 20? ", x); x > 20; end let q 0 = [ for x in [1; 25; 40; 5; 23] do if less. Than. Thirty x then yield x ] let q 1 = [ for x in q 0 do if more. Than. Twenty x then yield x ] F# Eager comprehensions for r in q 1 do Console. Write. Line("> {0}, ", r)

Exceptions var xs = new[]{ 9, 8, 7, 6, 5, 4, 3, 2, 1, Exceptions var xs = new[]{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; IEnumerable q = null; try { q = from x in xs select 1/x; } catch { q = new int[]; } foreach(var z in q) Console. Write. Line(z): // throws here

Disposing resources Func<string> Get. Contents = null; using( var file = File. System. Open. Disposing resources Func Get. Contents = null; using( var file = File. System. Open. Text. File. Reader(@"…")) { Get. Contents = () => file. Read. To. End(); } // surprise! an exception Console. Write. Line(Get. Contents());

Variable Instantiation var Delayed. Actions = new List<Func<int>>(); var s = Variable Instantiation var Delayed. Actions = new List>(); var s = ""; for (var i = 4; i < 7; i++) { Delayed. Actions. Add(delegate(){ return i; }); } for (var k = 0; k < Delayed. Actions. Count; k++) { s += Delayed. Actions[k](); } Console. Write. Line(s); Prints 777

Variable Instantiation var Delayed. Actions = new List<Func<int>>(); var s = Variable Instantiation var Delayed. Actions = new List>(); var s = ""; for (var i = 4; i < 7; i++) { var j = i; Delayed. Actions. Add(delegate(){ return j; }); } for (var k = 0; k < Delayed. Actions. Count; k++) { s += Delayed. Actions[k](); Prints 456 C# } Console. Write. Line(s); Prints 666 Java. Script

Variable Instantiation var Delayed. Actions = new List<Func<int>>(); var s = Variable Instantiation var Delayed. Actions = new List>(); var s = ""; var j; for (var i = 4; i < 7; i++) { j = i; Delayed. Actions. Add(delegate(){ return j; }); } for (var k = 0; k < Delayed. Actions. Count; k++) { s += Delayed. Actions[k](); } Prints 666 Console. Write. Line(s);

Object creation var o 1 = new object(); //58225482, for instance Console. Write. Line(o Object creation var o 1 = new object(); //58225482, for instance Console. Write. Line(o 1. Get. Hash. Code()); var o 2 = new object(); // 54267293, for instance Console. Write. Line(o 2. Get. Hash. Code()); Debug. Assert(o 1 != 02);

Concurrency new_cell(X) -> spawn(fun() -> cell(X) end). cell(Value) -> receive {set, New. Value} -> Concurrency new_cell(X) -> spawn(fun() -> cell(X) end). cell(Value) -> receive {set, New. Value} -> cell(New. Value); {get, Pid} -> Pid!{return, Value}, cell(Value); {dispose} -> {} end. set_cell(Cell, New. Value) -> Cell!{set, New. Value}. get_cell(Cell) -> Cell!{get, self()}, receive {return, Value} -> Value end. dispose_cell(Cell) -> Cell!{dispose}.

Page 165: There is a subtle error in on_exit and keep_alive … Write your 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.

*not* idempotent *not* pure GET …/person/age… GET …/clock/seconds… few interesting functions are idempotent *not* idempotent *not* pure GET …/person/age… GET …/clock/seconds… few interesting functions are idempotent

“Dishonest” types Not parametric! static T Identity<T>(this T me) { if(me. Get. Type() == “Dishonest” types Not parametric! static T Identity(this T me) { if(me. Get. Type() == typeof(Button)) throw new Exception(); var mini_me = me as string; if(mini_me != null) return “hello world”; Console. Write. Line(me. To. String()); return Activator. Create. Instance(); }

HOT Languages To the rescue HOT Languages To the rescue

less. Than. Thirty x = do { put. Str. Ln $ (show x)++ less. Than. Thirty x = do { put. Str. Ln $ (show x)++" less than 30? " ; return $ x < 30 } Couldn't match expected type `Bool' more. Than. Twenty x = do against inferred type `IO Bool' { put. Str. Ln $ (show a stmt of a list comprehension: In x)++" more than 20? " ; return $ x > 20 less. Than. Thirty x } In the expression: [x | x <- [1, 25, 50, . . ], less. Than. Thirty x] In the definition of `q 0': q 0 = [ x q 0 = 23 | x <- [1, 25, 50, 5, [x | ]x <- [1, 25, . . ], less. Than. Thirty x ] q 1 = [ x | x <- q 0 , more. Than. Twenty 20 ] Haskell

Forget about effects? Forget it! unsafe. Perform. IO : : IO a -> a Forget about effects? Forget it! unsafe. Perform. IO : : IO a -> a unsafe. Cast : : a -> b unsafe. Cast x = unsafe. Perform. IO do{ write. IORef castref x ; read. IORef castref } castref = unsafe. Perform. IO $ do{ new. IORef undefined } $

Cover up effects Cover up effects

Remove all effects (cannot make language more powerful by removing features) Remove all effects (cannot make language more powerful by removing features)

Don’t fight effects Embrace and extend Don’t fight effects Embrace and extend

Mens sana in corpore sano Mens sana in corpore sano

Mistake Date. Time. Now() Date. Time Type error !!!!!!!! Mistake Date. Time. Now() Date. Time Type error !!!!!!!!

Values vs Computation How to turn something mutable into something immutable? Time changes, the Values vs Computation How to turn something mutable into something immutable? Time changes, the clock does not

It’s just a collection All problems in computer science can be solved by another It’s just a collection All problems in computer science can be solved by another level of indirection D. Wheeler … 9: 15 9: 16 9: 17 9: 19 9: 21 9: 22 9: 23 9: 24 9: 25 9: 26 9: 27 9: 28 9: 29 9: 30 … AM AM AM AM

Mathematics is always right Mathematics is always right

Only one way to be pure, But many ways to be impure Multithreading STM<T> Only one way to be pure, But many ways to be impure Multithreading STM Exceptions Throws Destructive updates IO Reflection Mirror

data IO a Sideeffecting computation that yields a value of type a IO put. data IO a Sideeffecting computation that yields a value of type a IO put. Char : : Char -> IO () get. Char : : IO Char new. IORef : : a -> IO (IORef a) read. IORef : : IORef a -> IO a write. IORef : : IORef a -> IO () fork. IO : : IO a -> IO Thread. ID

Can Normal Programmers Understand Fundamentalist Functional Programming? Can Normal Programmers Understand Fundamentalist Functional Programming?

LINQ Monads are the secret sauce behind LINQ Bind IEnumerable<S> Select. Many<T, S>( IEnumerable<T> LINQ Monads are the secret sauce behind LINQ Bind IEnumerable Select. Many( IEnumerable src, Func> selector )

Java Throws clause void Foo(string s) throws Exception Explicit Effects Java Throws clause void Foo(string s) throws Exception Explicit Effects

Fundamentalist Functional Programming Abstract from evaluation order Make all effects explicit ==> Compiler can 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?

Call To Action Buy Graham Hutton’s book Nudge and nag MS to produce a Call To Action Buy Graham Hutton’s book Nudge and nag MS to produce a fundamentalist functional language

Buy Bryan John & Don’s book Nudge and nag MS to produce a fundamentalist Buy Bryan John & Don’s book Nudge and nag MS to produce a fundamentalist functional language

Thank You Thank You