Skip to content

Instantly share code, notes, and snippets.

@potmo
Created January 25, 2015 16:34
Show Gist options
  • Save potmo/83f33349fa00fa554f22 to your computer and use it in GitHub Desktop.
Save potmo/83f33349fa00fa554f22 to your computer and use it in GitHub Desktop.
Piping functions with operator overload
import UIKit
import Foundation
infix operator => { associativity right}
// Start with input
func => ( left: String, right: (String)->Void ) -> () -> Void {
func returnFunc () -> Void
{
right(left);
}
return returnFunc;
}
// Handling result
func => ( left: (String, (String)->Void)-> Void, right: (String)->Void ) -> (String)->() {
func returnFunc(result: String){
left(result, right);
}
return returnFunc;
}
func addAToEnd(input: String, done: (String) -> Void) -> Void
{
let output:String = input + "A";
done(output);
}
func padWithB(input: String, done: (String) -> Void) -> Void
{
let output:String = "B" + input + "B";
done(output);
}
func stringOutput(result: String) -> Void{
println("The final result was: " + result);
}
let pipe = "smack" =>
addAToEnd =>
addAToEnd =>
addAToEnd =>
addAToEnd =>
padWithB =>
stringOutput;
pipe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment