1
1
mirror of https://github.com/github/semantic.git synced 2024-12-12 04:58:02 +03:00
semantic/prototype/Doubt/Stream.swift

113 lines
2.8 KiB
Swift
Raw Normal View History

2015-09-17 21:12:39 +03:00
public enum Stream<A>: NilLiteralConvertible, SequenceType {
2015-09-16 18:42:49 +03:00
case Nil
indirect case Cons(A, Memo<Stream>)
2015-09-16 18:44:56 +03:00
public init<S: SequenceType where S.Generator.Element == A>(sequence: S) {
self = Stream(generator: sequence.generate())
}
public init<G: GeneratorType where G.Element == A>(var generator: G) {
self = Stream { generator.next() }
}
public init(_ f: () -> A?) {
2015-09-16 20:44:02 +03:00
self = f().map { Stream.Cons($0, Memo { Stream(f) }) } ?? Stream.Nil
}
public static func pure(a: A) -> Stream {
return .Cons(a, Memo(evaluated: .Nil))
}
2015-09-17 21:23:33 +03:00
public func analysis<B>(@noescape ifCons ifCons: (A, Memo<Stream>) -> B, @noescape ifNil: () -> B) -> B {
switch self {
case let .Cons(first, rest):
return ifCons(first, rest)
case .Nil:
return ifNil()
}
}
public var uncons: (first: A, rest: Memo<Stream>)? {
return analysis(ifCons: { $0 }, ifNil: { nil })
2015-09-16 18:44:56 +03:00
}
public var first: A? {
return uncons?.first
}
2015-09-17 21:26:25 +03:00
public var rest: Memo<Stream> {
return analysis(ifCons: { $1 }, ifNil: { Memo(evaluated: .Nil) })
2015-09-16 18:44:56 +03:00
}
2015-09-16 18:56:06 +03:00
public var isEmpty: Bool {
return uncons == nil
}
2015-09-16 19:27:23 +03:00
public func map<B>(transform: A -> B) -> Stream<B> {
return analysis(
2015-09-17 21:30:41 +03:00
ifCons: { .Cons(transform($0), $1.map { $0.map(transform) }) },
2015-09-17 21:34:26 +03:00
ifNil: const(nil))
2015-09-16 19:27:23 +03:00
}
2015-09-16 19:28:45 +03:00
2015-09-17 21:36:40 +03:00
public func flatMap<B>(transform: A -> Stream<B>) -> Stream<B> {
return analysis(
ifCons: { transform($0).concat($1.map { $0.flatMap(transform) }) },
ifNil: const(nil))
}
2015-09-16 19:28:45 +03:00
public func concat(other: Memo<Stream>) -> Stream {
2015-09-17 21:30:49 +03:00
return analysis(
ifCons: { .Cons($0, $1.map { $0.concat(other.value) }) },
ifNil: { other.value })
}
public func concat(other: Stream) -> Stream {
return concat(Memo(evaluated: other))
2015-09-17 21:14:01 +03:00
}
2015-09-24 22:10:56 +03:00
public func fold<Result>(initial: Result, combine: (A, Memo<Result>) -> Result) -> Result {
return analysis(
ifCons: { combine($0, $1.map { $0.fold(initial, combine: combine) }) },
ifNil: const(initial))
}
2015-09-25 19:22:31 +03:00
public static func unfold<State>(state: State, _ unspool: State -> (A, State)?) -> Stream {
return unspool(state).map { value, next in .Cons(value, Memo { self.unfold(next, unspool) }) } ?? .Nil
}
2015-09-24 22:10:56 +03:00
2015-09-25 20:57:35 +03:00
public func zipWith<S: SequenceType>(sequence: S) -> Stream<(A, S.Generator.Element)> {
return Stream<(A, S.Generator.Element)>.unfold((self, Stream<S.Generator.Element>(sequence: sequence))) {
guard let (x, xs) = $0.uncons, (y, ys) = $1.uncons else { return nil }
return ((x, y), (xs.value, ys.value))
}
}
2015-09-28 17:02:31 +03:00
public func take(n: Int) -> Stream {
return Stream.unfold((Memo(evaluated: self), n)) { stream, n in
guard let (x, xs) = stream.value.uncons else { return nil }
2015-09-28 17:28:44 +03:00
return n > 0
2015-09-28 17:02:31 +03:00
? (x, (xs, n - 1))
: nil
}
}
2015-09-17 21:12:39 +03:00
public init(nilLiteral: ()) {
self = .Nil
}
2015-09-16 19:28:45 +03:00
public func generate() -> AnyGenerator<A> {
var current = Memo(evaluated: self)
2015-09-16 19:28:45 +03:00
return anyGenerator {
2015-09-17 21:27:52 +03:00
let next = current.value.first
current = current.value.rest
2015-09-16 19:28:45 +03:00
return next
}
}
2015-09-16 18:42:49 +03:00
}