On 18 Dec 2014, at 06:27, 'Jason Stokes' via Swift Language <
swift-l...@googlegroups.com> wrote:
>
> I have an implementation of fold right (not intended to be maximally efficient or anything):
>
> Extension Array {
> private func foldrImp<U>(var iter: IndexingGenerator<Array<T>>, combine: (U, T) -> U, val: U) -> U {
> let next = iter.next()
> switch next {
> case .None:
> return val
> case .Some(let elm):
> return combine(foldrImp(iter, combine: combine, val:val), elm)
> }
> }
>
> func foldr<U>(initial: U, combine: (U, T) -> U) -> U {
> return foldrImp(self.generate(), combine: combine, val: initial)
> }
> }
>
> Two issues:
>
> In the signature to foldrImp, why can't I write?:
>
> private func foldrImp<U>(var iter: GeneratorOf<T>, combine: (U, T) -> U, val: U) -> U
You could do it this way, but then the caller would have to explicitly pass GeneratorOf(self.generate()). The easiest option is "var iter: Array.Generator". Generator is an associated type in the GeneratorType protocol, which is (typically) inferred as the result type of the generate() function, i.e. precisely what you want here.