Created
June 3, 2014 16:25
-
-
Save davidpdrsn/3afcee0327285be77fbd to your computer and use it in GitHub Desktop.
Fold function in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func foldl<T, E>(list:Array<T>, base:E, transform:(acc:E, item:T) -> E) -> E { | |
var result = base | |
for item in list { | |
result = transform(acc: result, item: item) | |
} | |
return result | |
} | |
func add(x:Int, y:Int) -> Int { | |
return x + y; | |
} | |
var numbers = [1,2,3,4,5] | |
var sum = foldl(numbers, 0, add) | |
println(sum) |
vlm
commented
Apr 18, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment