Created
February 26, 2019 17:25
-
-
Save pzel/17d4122c6bf2fd3a035d93270cf806c2 to your computer and use it in GitHub Desktop.
Maybe primitive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type MaybeT[A] is (A|None) | |
primitive Maybe | |
fun just[A: Any val](a: A) : MaybeT[A] => a | |
fun nothing[A: Any val]() : MaybeT[A] => None | |
fun fmap[A: Any val, B: Any val](v: MaybeT[A], f: {(A): B}): MaybeT[B] => | |
match v | |
| None => None | |
| let v' : A => f(v') | |
end | |
actor Main | |
new create(env: Env) => | |
let inc : {(U32) : U32} = {(a:U32) => a + 10} | |
// This is the only line you have to change (a. or b.) | |
// Whichever it is, your .fmapped code stays the same. | |
let maybeInt1 : MaybeT[U32] = Maybe.just[U32](1) | |
// let maybeInt1 : U32 = None // b. | |
let maybeInt11 = Maybe.fmap[U32, U32](maybeInt1, inc) | |
let maybeInt21 = Maybe.fmap[U32, U32](maybeInt11, inc) | |
let maybeString = Maybe.fmap[U32, String](maybeInt21, {(a) => a.string()}) | |
let maybePrint = Maybe.fmap[String, None](maybeString, {(a) => env.out.print(a)}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment