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

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

Embed Size (px)

Citation preview

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

Disclaimer:

The opinions expressed herein are my own

personal opinions and do not represent my

employer's view in any way.

Page 2: 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 = dynamicXXX = imperativeXXX = OO

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

What is a Functional Language

A language where functions are first-class citizens

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

EWD 1073

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

Easy likeSunday

morning

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

Dan MeyerWTF went

wrong?

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

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

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

Doing IO is a side-effect

let date = DateTime.Now()in (date, date)( DateTime.Now(), DateTime.Now())

DateTime.Now is not a mathematical

function

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

If I cannot even do IO

Fundamentalist

Functional Programmin

gis just

academic nonsense

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

Don’t eatTheYellowSnow!

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

Lazy evaluation and side-effects

static bool LessThanThirty(int x){ Console.Write("{0}? Less than 30;",x); return x < 30;}static bool MoretHanTwenty(int x){ Console.Write("{0}? More than 20;",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("[{0}];",r);

Order of side effects?

C#

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

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 do Console.WriteLine("> {0},", r)

F#Lazy

comprehensions

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

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

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

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

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

Func<string> GetContents = null;

using( var file = FileSystem.OpenTextFileReader(@"…")){ GetContents = () => file.ReadToEnd();}

// surprise! an exception Console.WriteLine(GetContents());

Disposing resources

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

var DelayedActions = new List<Func<int>>();var s = "";

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

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

var DelayedActions = new List<Func<int>>();var s = "";

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

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

var DelayedActions = new List<Func<int>>();var s = "";

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

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

var o1 = new object();//58225482, for instanceConsole.WriteLine(o1.GetHashCode());

var o2 = new object();// 54267293, for instanceConsole.WriteLine(o2.GetHashCode());

Debug.Assert(o1 != 02);

Object creation

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

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

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

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.

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

GET …/person/age…GET …/clock/seconds…

few interestingfunctions areidempotent

*not* idempotent*not* pure

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

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

parametric!

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

HOT Languages

To the rescue

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

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 <- [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 xIn the expression: [x | x <- [1, 25, 50, ....], lessThanThirty x]In the definition of `q0': q0 = [x | x <- [1, 25, ....], lessThanThirty x

Haskell

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

unsafePerformIO :: IO a -> a

unsafeCast :: a -> bunsafeCast x = unsafePerformIO $ do{ writeIORef castref x ; readIORef castref }

castref = unsafePerformIO $ do{ newIORef undefined }

Forget about effects? Forget it!

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

Cover upeffects

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

Remove all effects

(cannot make languagemore powerfulby removing features)

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

Don’t fight effects Embrace and extend

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

Mens sana in corpore sano

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

Mistake

DateTime.Now() DateTime

Type error!!!!!!!!!!!!!!!

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

Values vs Computation

How to turn something mutable into something immutable?

Time changes, the clock does not

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

…9:15 AM9:16 AM9:17 AM9:19 AM9:29 AM9:21 AM9:22 AM9:23 AM9:24 AM9:25 AM9:26 AM9:27 AM9:28 AM9:29 AM9:30 AM

It’s just a collection

All problems in computer science can be solved by

another level of indirection

D. Wheeler

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

Mathematics is always right

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

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

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

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

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

IO

data IO a

putChar :: Char -> IO ()getChar :: IO Char

newIORef :: a -> IO (IORef a)readIORef :: IORef a -> IO awriteIORef :: IORef a -> a -> IO ()

forkIO :: IO a -> IO ThreadID

Side-effecting

computation that yields a value of type

a

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

CanNormal

ProgrammersUnderstand

FundamentalistFunctional

Programming?

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

LINQMonads are the secret sauce behind LINQ

IEnumerable<S> SelectMany<T,S>( IEnumerable<T> src, Func<T, IEnumerable<S>> selector)

Bind

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

Java

Throws clause

void Foo(string s) throws Exception

ExplicitEffects

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

FundamentalistFunctional Programming

Abstract from evaluation orderMake all effects explicit==>Compiler can freely rearrange codeRuntime can execute in code any order

Call-by-name, or call-by-value version?

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

Call To ActionBuy Graham Hutton’s book

Nudge and nag MS to produce a fundamentalist functional language

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

Buy BryanJohn & Don’sbook

Nudge and nag MS to produce a fundamentalist functional language

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

Thank You

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