Skip to content

Instantly share code, notes, and snippets.

@philsquared
Last active August 29, 2015 14:02
Show Gist options
  • Save philsquared/51d28c8b364a16ed328a to your computer and use it in GitHub Desktop.
Save philsquared/51d28c8b364a16ed328a to your computer and use it in GitHub Desktop.
// Define an optional chaining pipeline operator
operator infix |> { associativity left }
func |> <T, U>(t: T?, f: (T) -> U? ) -> U? {
if let t1 = t { return f( t1 ) }
else { return nil }
}
// some dummy classes and functions to chain together
class C { let x = 7 }
class B { }
class A { }
func getB( a : A ) -> B? { return B() }
func getC( b : B ) -> C? { return C() }
let x = ( A() |> getB |> getC )?.x
println("x: \(x)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment