Skip to content

Instantly share code, notes, and snippets.

@Baudin999
Created March 21, 2016 11:51
Show Gist options
  • Save Baudin999/62f865dd7bca09213bb6 to your computer and use it in GitHub Desktop.
Save Baudin999/62f865dd7bca09213bb6 to your computer and use it in GitHub Desktop.
A simple Process MOnad in F#
type Process<'a> =
| Value of 'a
| Message of string
let quad a = a * a
let add a b = a + b
let print m =
match m with
| Value n -> printf "Number: %d\n" n
| Message m -> printf "Message: %s\n" m
m
let validateNumber a =
match a with
| even when a % 2 = 0 -> Value a
| _ -> Message "Should be an even number!"
// map :: (a -> b) -> (a -> Mb)
let map f d =
Value (f d)
// bind :: Ma -> (a -> Mb) -> Mb
let bind f d =
match d with
| Value i -> f i
| m -> m
let p = (map quad) >> (bind validateNumber) >> print
p 2
p 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment