Last active
July 23, 2019 11:32
-
-
Save aal89/d35a7d2056f4fcd3486d279ab927107b to your computer and use it in GitHub Desktop.
A pure functional way to blaze some sick clouds 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
// Higher order functions, being curried. Considered pure functional, because they don't have any side effects | |
// (mutate any given data). They just derive data from data. | |
let plus = { (first: Int) in { (second: Int) in first + second } } | |
let multiplesOf = { (first: Int) in { (second: Int) in second % first != 0 } } | |
// Just a simple first class function, not per se functional. Usable to reduce a collection of integers into the total. | |
let total: (Int, Int) -> Int = { $0 + $1 } | |
let calculation = (1...30) | |
.map(plus(2)) | |
.filter(multiplesOf(5)) | |
.reduce(0, total) | |
print("\(calculation) vape nationnn") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment