Last active
August 29, 2015 14:22
-
-
Save sebastiangrail/5d477c6e311483959292 to your computer and use it in GitHub Desktop.
Somewhat cleaner version of https://gist.github.com/ColinEberhardt/b4bf4e4566ffa88afcda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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