Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample code from this blog post: http://thepurecoder.com/more-on-fold/
func foldl1<A>(list: Array<A>, f: (A, A) -> A) -> A {
return foldl(list.head!, list: list.tail!, f: f)
}
extension Array {
var initial: Array<Element>? {
get {
if self.empty() { return nil }
return Array(dropLast(self))
}
}
}
func foldr<A,B>(acc: A, list: Array<B>, f: (B, A) -> A) -> A {
if list.empty() { return acc }
return foldr(f(list.last!, acc), list: list.initial!, f: f);
}
func foldr1<A>(list: Array<A>, f: (A, A) -> A) -> A {
return foldr(list.last!, list: list.initial!, f: f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment