I see that Swift offers convenient syntax for declaring curried functions. The manual gives partial function application as an example of where curried function will come in handy.
Can someone give me an example where partial function application can be useful? I know this is a general functional programming concept, but an example in Swift would be most appreciated.
Suppose you frequently want to check a number, i
, is a multiple of another value. What it might be a multiple of can change, but the rule for how to determine it is always the same: i % n == 0
.
You might write a function like this:
func isMultipleOf(#n: Int, #i: Int) -> Bool {
return i % n == 0
}
isMultipleOf(n: 2, i: 3) // false
isMultipleOf(n: 2, i: 4) // true
However, perhaps you find yourself frequently wanting to use this function with other "high-order" functions – that is, functions that take other functions as arguments, such as map
and filter
:
let r = 1...10
// use isMultipleOf to filter out the even numbers
let evens = filter(r) { isMultipleOf(n: 2, i: $0) }
// evens is now [2,4,6,8,10]
That use of isMultipleOf
looks a little clunky and hard to read, so maybe you define a new function, isEven
, in terms of isMultipleOf
to make it a bit clearer:
let isEven = { isMultipleOf(n: 2, i: $0) }
isEven(2) // true
isEven(3) // false
let evens = filter(r, isEven)
Now, suppose you declare isMultipleOf
a little differently, as a curried function:
func isMultipleOf(#n: Int)(#i: Int) -> Bool {
return i % n == 0
}
isMultipleOf
is now a function that takes a number, n
, and returns a new function that takes a number and checks if it's a multiple of n
.
You can now use it to declare isEven
like this:
let isEven = isMultipleOf(n: 2)
Or you could use it directly with filter like this:
let evens = filter(r, isMultipleOf(n: 2))
// just like before, evens is [2,4,6,8,10]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With