Skip to content

Instantly share code, notes, and snippets.

@mathonsunday
Created December 6, 2015 17:55
Show Gist options
  • Save mathonsunday/fde6a3358094fc925d87 to your computer and use it in GitHub Desktop.
Save mathonsunday/fde6a3358094fc925d87 to your computer and use it in GitHub Desktop.
First step is to desugar do notation. In this case we want to translate it to the bind (>>=) operator:
greeter =
name >>= ask
return ("hello, " ++ name ++ "!")
Now that it's desugared (not convinced I've done it correctly) I will translate it to Swift.
I'm relying on the Reader monad implementation in Swiftz (https://github.com/typelift/Swiftz/blob/968075391aedb15ec51ff9d5b7d4921b8fa3a3c6/Swiftz/Reader.swift)
Current code is:
func greeter(reader: Reader, name: String) {
name >>= Reader.ask
return ("hello, " ++ name ++ "!")
}
@mpurland
Copy link

mpurland commented Dec 9, 2015

with syntactic sugar:

let greeting = (hello() >>- runReader)?("World")
// Output: Hello World

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment