1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 08:54:07 +03:00

Add a Memo type.

This commit is contained in:
Rob Rix 2015-09-16 12:59:57 -04:00
parent 3bade61660
commit 8c16f0321b
2 changed files with 35 additions and 0 deletions

View File

@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
D432D4711BA9AC0B00F3FABC /* DiffTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D432D4701BA9AC0B00F3FABC /* DiffTests.swift */; };
D432D4731BA9C55300F3FABC /* Stream.swift in Sources */ = {isa = PBXBuildFile; fileRef = D432D4721BA9C55300F3FABC /* Stream.swift */; };
D432D4751BA9D6A400F3FABC /* Memo.swift in Sources */ = {isa = PBXBuildFile; fileRef = D432D4741BA9D6A400F3FABC /* Memo.swift */; };
D4AAE50E1B5AE22E004E581F /* Doubt.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4AAE4FD1B5AE22E004E581F /* Doubt.framework */; };
D4AAE5131B5AE22E004E581F /* DoubtTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE5121B5AE22E004E581F /* DoubtTests.swift */; };
D4AAE5401B5AE2D0004E581F /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE5351B5AE2D0004E581F /* Array.swift */; };
@ -37,6 +38,7 @@
/* Begin PBXFileReference section */
D432D4701BA9AC0B00F3FABC /* DiffTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiffTests.swift; sourceTree = "<group>"; };
D432D4721BA9C55300F3FABC /* Stream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Stream.swift; sourceTree = "<group>"; };
D432D4741BA9D6A400F3FABC /* Memo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Memo.swift; sourceTree = "<group>"; };
D4AAE4FD1B5AE22E004E581F /* Doubt.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Doubt.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D4AAE5011B5AE22E004E581F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D4AAE50D1B5AE22E004E581F /* DoubtTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DoubtTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@ -107,6 +109,7 @@
D4AAE53F1B5AE2D0004E581F /* Syntax.swift */,
D4AAE54B1B5AE42D004E581F /* Equatable.swift */,
D432D4721BA9C55300F3FABC /* Stream.swift */,
D432D4741BA9D6A400F3FABC /* Memo.swift */,
D4AAE5001B5AE22E004E581F /* Supporting Files */,
);
path = Doubt;
@ -247,6 +250,7 @@
D432D4731BA9C55300F3FABC /* Stream.swift in Sources */,
D4AAE5421B5AE2D0004E581F /* Doc.swift in Sources */,
D4AAE5461B5AE2D0004E581F /* Parse.swift in Sources */,
D432D4751BA9D6A400F3FABC /* Memo.swift in Sources */,
D4AAE5491B5AE2D0004E581F /* StringLiteralConvertible.swift in Sources */,
D4AAE5451B5AE2D0004E581F /* Operators.swift in Sources */,
);

View File

@ -0,0 +1,31 @@
public struct Memo<A> {
public var value: A {
return _value.value.value()
}
private var _value: MutableBox<Thunk<A>>
}
private final class MutableBox<A> {
init(_ value: A) {
self.value = value
}
var value: A
}
private enum Thunk<A> {
case Evaluated(A)
case Unevaluated(() -> A)
mutating func value() -> A {
switch self {
case let .Evaluated(a):
return a
case let .Unevaluated(f):
let a = f()
self = .Evaluated(a)
return a
}
}
}