Created
June 26, 2019 20:58
-
-
Save arashkashi/159fc072a5aff9a73cf088ae1d53e865 to your computer and use it in GitHub Desktop.
More advanced serialization operator
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
infix operator --> :AdditionPrecedence | |
enum Result<T> { | |
case success(T) | |
case fail(Error) | |
} | |
func --><InA, InAEnv,OutAEnv ,OutBEnv , OutA, OutB> | |
(first: @escaping ( InAEnv? , InA , @escaping (OutAEnv?, Result<OutA>) -> () ) -> (), | |
second: @escaping (OutAEnv? , OutA, @escaping (OutBEnv?, Result<OutB>) -> () ) -> ()) -> | |
(InAEnv?, InA, @escaping (OutBEnv?, Result<OutB>) -> ()) -> () { | |
return { (inAEnv: InAEnv?, input: InA, compB: @escaping (OutBEnv?, Result<OutB>) -> () ) in | |
first(inAEnv, input, { outAEnv, resultA in | |
print("Function: \(#function), line: \(#line)") | |
switch resultA { | |
case .success(let result): | |
second(outAEnv, result, compB) | |
case .fail(let error): | |
compB(nil, .fail(error)) | |
} | |
}) | |
} | |
} | |
func a(env: Any?, a: Int, completion: @escaping (Any?, Result<Int>) -> ()) { | |
completion(nil, .success(a+1)) | |
} | |
func b(env: Any?, b: Int, completion: @escaping (Any?, Result<String>) -> ()) { | |
completion(nil, .success("Ahmad")) | |
} | |
func c(env: Any?, c: String, completion: @escaping (Any?, Result<Int>) -> ()) { | |
completion(nil, .success(5)) | |
} | |
let result = a --> b --> c | |
result(nil, 4, { _, x in | |
switch x { | |
case .fail(_): break | |
case .success(let antwort): | |
print(antwort) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment