From b4a95647474238705679433b4e72395e643d7ba1 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 14 Oct 2015 18:47:20 -0400 Subject: [PATCH] TermType has a size property. --- prototype/Doubt/TermType.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/prototype/Doubt/TermType.swift b/prototype/Doubt/TermType.swift index efec72b2c..ca29409f8 100644 --- a/prototype/Doubt/TermType.swift +++ b/prototype/Doubt/TermType.swift @@ -10,6 +10,21 @@ extension TermType { public func cata(transform: Syntax -> Result) -> Result { return self |> ({ $0.out } >>> { $0.map { $0.cata(transform) } } >>> transform) } + + + /// The size of the receiver. + public var size: Int { + return cata { + switch $0 { + case .Leaf: + return 1 + case let .Indexed(i): + return i.reduce(1, combine: +) + case let .Keyed(k): + return k.values.reduce(1, combine: +) + } + } + } }