1
1
mirror of https://github.com/github/semantic.git synced 2024-11-25 21:43:07 +03:00
semantic/prototype/Doubt/Cofree.swift

29 lines
514 B
Swift
Raw Normal View History

2015-10-14 16:34:18 +03:00
// Copyright © 2015 GitHub. All rights reserved.
2015-10-14 16:43:51 +03:00
public enum Cofree<A, B> {
2015-10-14 16:56:46 +03:00
indirect case Unroll(B, Syntax<Cofree, A>)
2015-10-14 16:59:04 +03:00
public var unwrap: Syntax<Cofree, A> {
switch self {
case let .Unroll(_, rest):
return rest
}
}
2015-10-14 16:34:18 +03:00
}
2015-10-14 16:43:23 +03:00
// MARK: - Comonad
extension Cofree {
2015-10-14 17:01:19 +03:00
public var extract: B {
2015-10-14 16:43:23 +03:00
switch self {
2015-10-14 16:43:51 +03:00
case let .Unroll(b, _):
2015-10-14 16:43:23 +03:00
return b
}
}
2015-10-14 17:00:16 +03:00
2015-10-14 17:01:26 +03:00
public func extend<Other>(transform: Cofree -> Other) -> Cofree<A, Other> {
2015-10-14 17:00:16 +03:00
return .Unroll(transform(self), unwrap.map { $0.extend(transform) })
}
2015-10-14 16:43:23 +03:00
}