Last active
November 13, 2020 20:49
-
-
Save RuiAAPeres/080479a8e547393fc66d658ad36c7297 to your computer and use it in GitHub Desktop.
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
public typealias CompletionHandler = Function<Void, Void> | |
public typealias SideEffect<Input> = Function<Input, Void> | |
public struct Function<Input, Output>: Identifiable { | |
public let id: String | |
internal let f: (Input) -> Output | |
public init( | |
id: String, | |
f: @escaping (Input) -> Output | |
) { | |
self.id = id | |
self.f = f | |
} | |
public init(f: @escaping (Input) -> Output) { | |
self.init(id: generateIdentifier(), f: f) | |
} | |
public func apply(input: Input) -> Output { | |
f(input) | |
} | |
public func contraMap<T>(_ g: @escaping (T) -> Input) -> Function<T, Output> { | |
Function<T, Output>(f: g >>> self.apply(input:)) | |
} | |
public func map<T>(_ g: @escaping (Output) -> T) -> Function<Input, T> { | |
Function<Input, T>(f: self.apply(input:) >>> g) | |
} | |
public func callAsFunction(_ input: Input) -> Output { | |
f(input) | |
} | |
} | |
public extension Function where Input == Void, Output == Void { | |
func apply() { | |
f(()) | |
} | |
} | |
public extension Function where Input == Void { | |
func apply() -> Output { | |
f(()) | |
} | |
} | |
public extension Function where Output == Void { | |
static var empty: Function { | |
return Function { _ in } | |
} | |
} | |
extension Function: Equatable { | |
public static func == (lhs: Function<Input, Output>, rhs: Function<Input, Output>) -> Bool { | |
lhs.id == rhs.id | |
} | |
} | |
public extension Function where Input == Output { | |
static var identity: Function { | |
return Function { $0 } | |
} | |
} | |
public func >>> <A, B, C>(a: Function<A, B>, b: Function<B, C>) -> Function<A, C> { | |
return Function(f: a.f >>> b.f) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment