Skip to content

Instantly share code, notes, and snippets.

@harlanhaskins
Last active May 5, 2016 03:08
Show Gist options
  • Save harlanhaskins/90b01e139788181a3de6d42495c18732 to your computer and use it in GitHub Desktop.
Save harlanhaskins/90b01e139788181a3de6d42495c18732 to your computer and use it in GitHub Desktop.

Consider this Swift:

let addr: String? = person?.job?.address

In this case, if person is nil, it returns nil, otherwise it binds the job, and repeats.

This same thing is expressed in Haskell as:

addrOf person = person >>= (\p -> ((job p) >>= (\job -> (address job))))

Or the much less unwieldy:

addrOf person = do
  p <- person
  j <- job person
  return $ address j
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment