Created
December 20, 2016 10:30
-
-
Save ukitaka/15ec8fa82bb3a792252f00a973425ed0 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
| struct Prism<S, A> { | |
| private let getOption: (S) -> A? | |
| private let reverseGet: (A) -> S | |
| init(getOption: @escaping (S) -> A?, reverseGet: @escaping (A) -> S) { | |
| self.getOption = getOption | |
| self.reverseGet = reverseGet | |
| } | |
| } | |
| func somePrism<A>() -> Prism<A?, A> { | |
| return Prism<A?, A>( | |
| getOption: { optA in | |
| return optA | |
| }, | |
| reverseGet: { a in | |
| return .some(a) | |
| } | |
| ) | |
| } | |
| func nonePrism<A>() -> Prism<A?, Void> { | |
| return Prism<A?, Void>( | |
| getOption: { optA in | |
| switch optA { | |
| case .none: return .some(()) | |
| case .some(let a): return .none | |
| } | |
| }, | |
| reverseGet: { _ in | |
| return .none | |
| } | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment