Last active
May 7, 2016 22:22
-
-
Save charlieegan3/c68b3baf4c70c990b48fbec0022bf113 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//: Playground - noun: a place where people can play | |
import Darwin | |
import Foundation | |
enum Result<T> { | |
case Ok(T) | |
case Err(String) | |
} | |
infix operator >>> { associativity left } | |
func >>> <A, B, C>(f: B -> C, g: A -> B) -> A -> C { | |
return { x in f(g(x)) } | |
} | |
func gamble(probability:UInt32) -> Bool { | |
return probability > UInt32(arc4random_uniform(101)) | |
} | |
func task_1(input:Result<String>) -> Result<String> { | |
switch input { | |
case .Ok(let string): | |
if gamble(30) { | |
return Result.Err("Task 1 error, string: " + string) | |
} else { | |
return Result.Ok(string.uppercaseString) | |
} | |
case .Err(let string): | |
return Result.Err(string) | |
} | |
} | |
func task_2(input:Result<String>) -> Result<String> { | |
switch input { | |
case .Ok(let string): | |
if gamble(30) { | |
return Result.Err("Task 2 error, string: " + string) | |
} else { | |
return Result.Ok("OUTPUT") | |
} | |
case .Err(let string): | |
return Result.Err(string) | |
} | |
} | |
func task_3(input:Result<String>) -> Result<String> { | |
switch input { | |
case .Ok(let string): | |
if gamble(30) { | |
return Result.Err("Task 3 error, string: " + string) | |
} else { | |
return Result.Ok(string.capitalizedString) | |
} | |
case .Err(let string): | |
return Result.Err(string) | |
} | |
} | |
let pipeline = task_3 >>> task_2 >>> task_1 | |
for index in 1...10 { | |
print(pipeline(Result.Ok("Input"))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment