Last active
August 29, 2015 14:02
-
-
Save plumhead/a4955117bbda5f6bab6f to your computer and use it in GitHub Desktop.
Swift Fib Pipe
This file contains 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
// 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