Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
# i am using capital letters for type variables
# i am also probably over-annotating types just to try to be clear what is going on
# assume we have a function type
f1 ^A ^B = ...magic..
# simple pair type
pair ^A = match [^x1: A, ^x2: A]
# option type
# so what i want is just a type that either has a value or doesn't, and which
# can test if a value is present, map that value into a new value, etc. crucially
# unlike normal value + null, you can't just access a value and get an NPE.
# expressed in a different syntax, i want something like:
# some(string, "cat").isDefined -> true
# none(string).isDefined -> false
# both have type option(string)
# some(int, 4).map(x -> x + 4.0) = some(double, 8.0)
# none(int).map(x -> x + 4.0) = none(double)
# both go from option(int) to option(double)
# i don't care if some(A) and none(A) are types,
# but i want:
# option(A) = Some(A, value) | none(A)
option ^A = [
.isDefined [.some, A, _] = true: bool
.isDefined [.none, A] = false: bool
.map ^B (^f: (f1 A B)) [.some, A, x] = some B x: (option B)
.map ^B (^f: (f1 A B)) [.none, A] = none B: (option B)
.getOrElse [.some, A, x] ^default: A = x: A
.getOrElse [.none, A] ^default: A = default: A
some ^A ^x = [.some, A, x]
none ^A = [.none, A]
# i need to wire some/none up to extend option to inherit its functions
# but i'm not clear on how to do that while also having them create
# the tuples they need for their structure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment