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
|
|
|
|
|
|
|
func extend<Other>(transform: Cofree -> Other) -> Cofree<A, Other> {
|
|
|
|
return .Unroll(transform(self), unwrap.map { $0.extend(transform) })
|
|
|
|
}
|
2015-10-14 16:43:23 +03:00
|
|
|
}
|