Last active
February 8, 2017 02:10
-
-
Save ukitaka/39eacb94f4ccf1493fa46e3f7f6cf50d to your computer and use it in GitHub Desktop.
Recoverable
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
| func const<A, B>(_ a: A) -> (B) -> A { | |
| return { _ in a } | |
| } | |
| struct LazyRecoverable<A> { | |
| private let original: A | |
| private var updatef: ((A) -> A)? | |
| private var mutableCopy: A | |
| init(_ original: A) { | |
| self.original = original | |
| self.mutableCopy = original | |
| } | |
| mutating func update(_ f: @escaping (A) -> A) { | |
| if let updatef = self.updatef { | |
| self.mutableCopy = updatef(mutableCopy) | |
| } | |
| self.updatef = f | |
| } | |
| func commit() -> A { | |
| let f = updatef ?? const(mutableCopy) | |
| return f(mutableCopy) | |
| } | |
| func rollback() -> A { | |
| return original | |
| } | |
| } |
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 Recoverable<A> { | |
| private let original: A | |
| private var mutableCopy: A | |
| init(_ original: A) { | |
| self.original = original | |
| self.mutableCopy = original | |
| } | |
| mutating func update(_ f: (A) -> A) { | |
| self.mutableCopy = f(mutableCopy) | |
| } | |
| func commit() -> A { | |
| return mutableCopy | |
| } | |
| func rollback() -> A { | |
| return original | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment