1
1
mirror of https://github.com/github/semantic.git synced 2024-11-29 02:44:36 +03:00

CofreeType values can be zipped.

This commit is contained in:
Rob Rix 2015-10-22 18:06:35 -04:00
parent 6bc8fe4285
commit 7b57a9ce17

View File

@ -124,6 +124,20 @@ extension CofreeType {
public static func coiterate(unfold: Annotation -> Syntax<Annotation, Leaf>)(_ seed: Annotation) -> Self {
return (Wrap(seed) <<< { $0.map(coiterate(unfold)) } <<< unfold) <| seed
}
public static func zip(a: Self, _ b: Self) -> Cofree<Leaf, (Annotation, Annotation)>? {
let annotations = (a.extract, b.extract)
switch (a.unwrap, b.unwrap) {
case let (.Leaf, .Leaf(b)):
return Cofree(annotations, .Leaf(b))
case let (.Indexed(a), .Indexed(b)):
return Cofree(annotations, .Indexed(Swift.zip(a, b).flatMap(zip)))
case let (.Keyed(a), .Keyed(b)):
return Cofree(annotations, .Keyed(Dictionary(elements: b.keys.flatMap { key in zip(a[key]!, b[key]!).map { (key, $0) } })))
default:
return nil
}
}
}
extension Cofree: CofreeType {}