Skip to content

Instantly share code, notes, and snippets.

@adam-zethraeus
Created January 8, 2022 22:43
Show Gist options
  • Save adam-zethraeus/29db65ec7469946cc9ab5473a3468a1f to your computer and use it in GitHub Desktop.
Save adam-zethraeus/29db65ec7469946cc9ab5473a3468a1f to your computer and use it in GitHub Desktop.
(Flat)Mappable Boolean
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