Skip to content

Instantly share code, notes, and snippets.

@washort
Created June 29, 2017 16:55
Show Gist options
  • Save washort/87d88a82c4f5d8cdf92bbb7f49de4a3e to your computer and use it in GitHub Desktop.
Save washort/87d88a82c4f5d8cdf92bbb7f49de4a3e to your computer and use it in GitHub Desktop.
monad example in Monte using control blocks
exports (main)
def id(x) as DeepFrozen:
return x
object identity as DeepFrozen:
"The identity monad."
to unit(x):
return x
to "bind"(action, f):
return f(action)
to control(operator :Str, argArity :Int, paramArity :Int, block):
return switch ([operator, paramArity]):
match ==["do", 1]:
# The inner value and our action upon it.
def [[value], lambda] := block()
object controller:
to control(operator, argArity, paramArity, block):
def [_, nextLambda] := block()
def identityBlock():
return [[value], fn x, ej {
nextLambda(lambda(x, ej), ej)
}]
return identity.control(operator, argArity,
paramArity, identityBlock)
to controlRun():
return lambda(value, null)
match msg:
throw(`Unrecognized control operator $msg`)
def main(_) as DeepFrozen:
traceln(identity (2) do x { x + 3 } do y { y + 3 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment