1
1
mirror of https://github.com/github/semantic.git synced 2024-12-25 07:55:12 +03:00

Inline reduction in Free.sum.

This commit is contained in:
Rob Rix 2015-11-12 16:55:11 -05:00
parent 2963a76943
commit f2004a22b8

View File

@ -75,7 +75,18 @@ public enum Free<Leaf, Annotation, Value>: CustomDebugStringConvertible {
/// Returns a function which sums `Free`s by first `transform`ing `Pure` values into integers, and then summing these.
public static func sum(@noescape transform: Value -> Int)(_ free: Free) -> Int {
return free.map(transform).reduce(0, combine: +)
return free.map(transform).cata {
switch $1 {
case .Leaf:
return 0
case let .Indexed(a):
return a.reduce(0, combine: +)
case let .Fixed(a):
return a.reduce(0, combine: +)
case let .Keyed(a):
return a.values.reduce(0, combine: +)
}
}
}