Skip to content

Instantly share code, notes, and snippets.

@aal89
Last active July 23, 2019 11:32
Show Gist options
  • Save aal89/d35a7d2056f4fcd3486d279ab927107b to your computer and use it in GitHub Desktop.
Save aal89/d35a7d2056f4fcd3486d279ab927107b to your computer and use it in GitHub Desktop.
A pure functional way to blaze some sick clouds in Swift.
// 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