Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created July 18, 2015 05:18
Show Gist options
  • Save jweinst1/ef1fb0b3cc5ae57926cd to your computer and use it in GitHub Desktop.
Save jweinst1/ef1fb0b3cc5ae57926cd to your computer and use it in GitHub Desktop.
In swift, closures that take the count, sum, and mean of an array in swift.
// sums all the integers in an array
var ArraySum = {(var list:Array<Int>) -> Int in
var sum = 0
for element in list {
sum += element
}
return sum
}
// uses a for loop to count the integers in an array
var ArrayCount = {(var list:Array<Int>) -> Int in
var sum = 0
for element in list {
sum += 1
}
return sum
}
// takes the mean of an integer array
var Arraymean = {(var list:Array<Int>) -> Int in ArraySum(list)/ArrayCount(list)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment