Skip to content

Instantly share code, notes, and snippets.

@matthewphilyaw
Created December 27, 2015 02:19
Show Gist options
  • Save matthewphilyaw/bc8c04f58fc72d6d91a2 to your computer and use it in GitHub Desktop.
Save matthewphilyaw/bc8c04f58fc72d6d91a2 to your computer and use it in GitHub Desktop.
open System
type Maybe<'a> =
| Just of 'a
| Nothing
let Unit (x: 'a) : Maybe<'a> =
Just x
let Bind (f: 'a -> Maybe<'b>) (mx: Maybe<'a>) =
match mx with
| Just x -> f x
| Nothing -> Nothing
let Example1 x =
Unit(x)
|> Bind (fun x -> Unit(x + 4))
|> Bind (fun x -> Unit(x * 100))
|> Bind (fun x -> if x >= 2400 then Unit(x) else Nothing)
let Example2 x =
Unit(x)
|> Bind (fun x -> if x = 0 then Nothing else Unit(x))
|> Bind (fun x -> Unit(50 / x))
let PrintVal (x:Maybe<'a>) : unit =
match x with
| Just x -> Console.WriteLine(x.ToString())
| Nothing -> Console.WriteLine("No value returned")
[<EntryPoint>]
let main argv =
20 |> Example1 |> PrintVal
10 |> Example1 |> PrintVal
0 |> Example2 |> PrintVal
10 |> Example2 |> PrintVal
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment