Tokyo F# meetup 14-08-03

  • View
    970

  • Download
    21

Embed Size (px)

DESCRIPTION

Functional programming with F# (FSharp)

Text of Tokyo F# meetup 14-08-03

  • Functional thinking, a universal tool for the digital world Nicolas Rolland August 3, 2014 Nicolas Rolland Functional prototyping August 3, 2014 1 / 34
  • Table of Contents 1 Types why types ? Warm-up exercices IEnumerable and IObservable 2 Universals, programming against the interface and existentials universals it sounds like L in SOLID .. existentials 3 Prototyping the functional way Parser combinator Nicolas Rolland Functional prototyping August 3, 2014 2 / 34
  • Table of Contents 1 Types why types ? Warm-up exercices IEnumerable and IObservable 2 Universals, programming against the interface and existentials universals it sounds like L in SOLID .. existentials 3 Prototyping the functional way Parser combinator Nicolas Rolland Functional prototyping August 3, 2014 3 / 34
  • Types What is a type ? Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
  • Types What is a type ? Type A type is a set of values. Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
  • Types What is a type ? Type A type is a set of values. why do we need to think of it in typed functional programming ? a value is a value e.g. 1 Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
  • Types What is a type ? Type A type is a set of values. why do we need to think of it in typed functional programming ? a value is a value e.g. 1 a function is a value too ! let plus = (+) //fun x y -> x + y let plusone = plus 1 //fun x -> x + 1 let three = plusone 2 //3 Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
  • Types What is a type ? Type A type is a set of values. why do we need to think of it in typed functional programming ? a value is a value e.g. 1 a function is a value too ! let plus = (+) //fun x y -> x + y let plusone = plus 1 //fun x -> x + 1 let three = plusone 2 //3 hence everything is a value, everything has a type ! Nicolas Rolland Functional prototyping August 3, 2014 4 / 34
  • Using types Nicolas Rolland Functional prototyping August 3, 2014 5 / 34
  • Using types by restriction we get correctness - The computer tells us the errors Nicolas Rolland Functional prototyping August 3, 2014 5 / 34
  • Using types by restriction we get correctness - The computer tells us the errors restriction tells readers a specication - We can communicate to other people Nicolas Rolland Functional prototyping August 3, 2014 5 / 34
  • Using types by restriction we get correctness - The computer tells us the errors restriction tells readers a specication - We can communicate to other people Types should be helping you, not get in your way. No need to write the types in F#. Less work AND more security ! You can write the types for the purpose of communication with others Nicolas Rolland Functional prototyping August 3, 2014 5 / 34
  • Warm-up exercices Nicolas Rolland Functional prototyping August 3, 2014 6 / 34
  • what can I do with a value of this type ? Type1 () T Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
  • what can I do with a value of this type ? Type1 () T let a = (fun () -> 3) : unit -> int Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
  • what can I do with a value of this type ? Type1 () T let a = (fun () -> 3) : unit -> int lazy values ! Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
  • what can I do with a value of this type ? Type1 () T let a = (fun () -> 3) : unit -> int lazy values ! let delayedValue = fun () -> someComputationForLater()) T = () T Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
  • what can I do with a value of this type ? Type1 () T let a = (fun () -> 3) : unit -> int lazy values ! let delayedValue = fun () -> someComputationForLater()) T = () T let delay x = fun () x : T (() T) let force f = f () : (() T) T delay force = Id()T force delay = IdT Nicolas Rolland Functional prototyping August 3, 2014 7 / 34
  • what can I do with a value of this type ? Type2 type Type2 < T >= | Case1 of (T Type2 < T >) | Case0 Nicolas Rolland Functional prototyping August 3, 2014 8 / 34
  • what can I do with a value of this type ? Type2 type Type2 < T >= | Case1 of (T Type2 < T >) | Case0 let a = Case1(1,Case1(2,Case1(3,Case0))) Nicolas Rolland Functional prototyping August 3, 2014 8 / 34
  • what can I do with a value of this type ? Type2 type Type2 < T >= | Case1 of (T Type2 < T >) | Case0 let a = Case1(1,Case1(2,Case1(3,Case0))) This is a list ! match l with | Case1(value, rest) -> ... //do something with value | Case0 -> ... //do something else Nicolas Rolland Functional prototyping August 3, 2014 8 / 34
  • what can I do with a value of this type ? Type3 type RComp < T >= | NotYet of (() RComp < T >) | Finished of T Nicolas Rolland Functional prototyping August 3, 2014 9 / 34
  • what can I do with a value of this type ? Type3 type RComp < T >= | NotYet of (() RComp < T >) | Finished of T let c = NotYet(fun () -> (*first part*) NotYet (fun () -> (*second part*) Finished pi)) Nicolas Rolland Functional prototyping August 3, 2014 9 / 34
  • what can I do with a value of this type ? Type3 type RComp < T >= | NotYet of (() RComp < T >) | Finished of T let c = NotYet(fun () -> (*first part*) NotYet (fun () -> (*second part*) Finished pi)) Resumable computation ! Nicolas Rolland Functional prototyping August 3, 2014 9 / 34
  • can you recognize IEnumerable ? What is a value of type IEnumerable ? Nicolas Rolland Functional prototyping August 3, 2014 10 / 34
  • can you recognize IEnumerable ? What is a value of type IEnumerable ? a sequence of items Nicolas Rolland Functional prototyping August 3, 2014 10 / 34
  • can you recognize IEnumerable ? What is a value of type IEnumerable ? a sequence of items Item type Item of T = | Finished | Value of T Nicolas Rolland Functional prototyping August 3, 2014 10 / 34
  • can you recognize IEnumerable ? What is a value of type IEnumerable ? a sequence of items Item type Item of T = | Finished | Value of T IEnumerable type IEnumerable < T > = () (() Item of T) Nicolas Rolland Functional prototyping August 3, 2014 10 / 34
  • Object Oriented way Nicolas Rolland Functional prototyping August 3, 2014 11 / 34
  • Object Oriented way public interface IEnumerator { bool MoveNext(); Object Current { get; } void Reset(); } public interface IEnumerable { IEnumerator GetEnumerator(); } How can I read the protocol ? Should I call MoveNext rst ? What is the value of current after enumeration is nished ? How can I make sure current is not used after enumeration is nished ? Nicolas Rolland Functional prototyping August 3, 2014 11 / 34
  • IObservable / Reactive type IObservable < T > = (Item of T ()) () Nicolas Rolland Functional prototyping August 3, 2014 12 / 34
  • IObservable / Reactive type IObservable < T > = (Item of T ()) () IEnumerable type IEnumerable < T > = () (() Item of T) Dont they look similar .... ? Nicolas Rolland Functional prototyping August 3, 2014 12 / 34
  • IObservable / Reactive type IObservable < T > = (Item of T ()) () IEnumerable type IEnumerable < T > = () (() Item of T) Dont they look similar .... ? why ? Nicolas Rolland Functional prototyping August 3, 2014 12 / 34
  • What is a type - take 2 What is a type and why do we need to think of it ? Nicolas Rolland Functional prototyping August 3, 2014 13 / 34
  • What is a type - take 2 What is a type and why do we need to think of it ? Type A type is a computable specication Nicolas Rolland Functional prototyping August 3, 2014 13 / 34
  • What is a type - take 2 What is a type