Skip to content

Instantly share code, notes, and snippets.

@ukitaka
Created December 20, 2016 10:30
Show Gist options
  • Select an option

  • Save ukitaka/15ec8fa82bb3a792252f00a973425ed0 to your computer and use it in GitHub Desktop.

Select an option

Save ukitaka/15ec8fa82bb3a792252f00a973425ed0 to your computer and use it in GitHub Desktop.
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