Created
January 21, 2021 14:23
-
-
Save RuiAAPeres/a7b26be44653487324c78da76c0c684f 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
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