Skip to content

Instantly share code, notes, and snippets.

@harlanhaskins
Last active April 12, 2016 22:59
Show Gist options
  • Save harlanhaskins/9fa0242f44d34fe2244af6ad05ccc7b5 to your computer and use it in GitHub Desktop.
Save harlanhaskins/9fa0242f44d34fe2244af6ad05ccc7b5 to your computer and use it in GitHub Desktop.
// note the signature => (T?, T -> U?) -> U?
func flatMap<T, U>(value: T?, transform: T -> U?) -> U? {
switch value {
case .None: return .None
case .Some(let contained): return transform(contained)
}
}
// different from this => (T?, T -> U) -> U?
func map<T, U>(value: T?, transform: T -> U) -> U? {
switch value {
case .None: return .None
case .Some(let contained): return Optional.Some(transform(contained))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment