Last active
February 26, 2019 10:05
-
-
Save pzel/9d8faa177a206d225cc3de09bc972ea6 to your computer and use it in GitHub Desktop.
A maybe type, with Functor interface, in Pony
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
class val Maybe[A: Any val] | |
let _v : (A | None) | |
new val just(a: A) => _v = a | |
new val nothing() => _v = None | |
fun val fmap[B: Any val](f: {(A): B}) : Maybe[B] => | |
match _v | |
| None => Maybe[B].nothing() | |
| let v' : A => Maybe[B].just(f(v')) | |
end | |
interface Functor[A: Any val] | |
fun fmap[B: Any val](f: {(A): B}) : Functor[B] | |
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 : Maybe[U32] = Maybe[U32].just(1) // a. | |
// let maybeInt1 : Maybe[U32] = Maybe[U32].nothing() // b. | |
maybeInt1.fmap[U32](inc) | |
.fmap[U32](inc) | |
.fmap[String]({(a: U32) => a.string()}) | |
.fmap[None]({(a: String) => env.out.print(a)}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment