Skip to content

Instantly share code, notes, and snippets.

@sebastiangrail
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastiangrail/5d477c6e311483959292 to your computer and use it in GitHub Desktop.
Save sebastiangrail/5d477c6e311483959292 to your computer and use it in GitHub Desktop.
// make strings appendable
extension String {
func append (other: String) -> String {
return self + " " + other
}
}
// Chainable with append method. Note that strings aren't mutated
"Hi,"
.append("how")
.append("are")
.append("you?")
// Function to append b to a
func appendString (a: String, b: String) -> String {
return a.append(b)
}
// weird
appendString("Hi,", appendString("how", appendString("are", "you?")))
// slightly less weird
appendString(appendString(appendString("Hi,", "how"), "are"), "you?")
// Curried function. Note that the arguments are swapped compared to the uncurried version
func append (appendige: String)(_ receiver: String) -> String {
return receiver.append(appendige)
}
// Even worse than before
append("Hi,")(append("how")(append("are")("you?")))
// pipe forward as backwards function application:
// value |> function is equivalent to function(value)
// works with *any* types
infix operator |> { associativity left }
func |> <T, R> (value: T, transform: T -> R) -> R {
return transform(value)
}
// This is nice, we can even |> println at the end to print the result
"Hi,"
|> append("how")
|> append("are")
|> append("you?")
|> println
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment