Created
January 8, 2022 22:43
-
-
Save adam-zethraeus/29db65ec7469946cc9ab5473a3468a1f to your computer and use it in GitHub Desktop.
(Flat)Mappable Boolean
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
import Foundation | |
public struct If { | |
let ifTrue: Optional<Void> | |
public init(_ condition: @autoclosure () -> Bool) { | |
self.ifTrue = condition() ? .some(()) : .none | |
} | |
public func map<T>(_ transform: () -> T) -> T? { | |
ifTrue.map(transform) | |
} | |
public func flatMap<T>(_ transform: () -> T?) -> T? { | |
ifTrue.flatMap(transform) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment