Skip to content

Instantly share code, notes, and snippets.

@plumhead
Last active August 29, 2015 14:02
Show Gist options
  • Save plumhead/a4955117bbda5f6bab6f to your computer and use it in GitHub Desktop.
Save plumhead/a4955117bbda5f6bab6f to your computer and use it in GitHub Desktop.
Swift Fib Pipe
// a copy of the f# fib implementation
func fib(n : Int) -> Int {
if n == 0 || n == 1 {
return n
}
else {
return fib(n - 1) + fib(n - 2)
}
}
// A simple print output function
let output = {(n: Array<Int>) -> () in
for (index,value) in enumerate(n) {
println("[\(index)] is \(value)")
}
}
// Take an array of Ints, run fib on each in turn, filter just those %2 and then output the result
[1,2,3,4,5,6,7,8,9,10]
|> map(fib)
|> filter({$0 % 2 == 0})
|> output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment