Skip to content

Instantly share code, notes, and snippets.

@sergueyarellano
Created May 13, 2021 22:21
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 sergueyarellano/406f989b6547c47eb4baeae3dc897800 to your computer and use it in GitHub Desktop.
Save sergueyarellano/406f989b6547c47eb4baeae3dc897800 to your computer and use it in GitHub Desktop.
func pipe<T>(_ fns: (T) -> (T)...) -> (T) -> (T) {
return { (input) in fns.reduce(input, {value, fn in fn(value)})}
}
func addPrefix(_ prefix: String) -> (String) -> (String) {
return {(inputStr) in "\(prefix) \(inputStr)" }
}
func addSuffix(_ suffix: String) -> (String) -> (String) {
return {(inputStr) in "\(inputStr), \(suffix)" }
}
pipe(addOne, addOne)(1) // 3
pipe(addOne, addTwo, addOne)(1) // 5
pipe(addPrefix("The honorable"), addSuffix("himself"))("Curmodgeon") // "The honorable Curmodgeon, himself"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment