upon
¶core.lambda.
upon
(f, g)¶Returns: | A binary function f with arguments transformed by g . |
---|
(β → β → γ) → (α → β) → (α → α → γ)
Applies an unary function to both arguments of a binary function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // Sorting an array of pairs by the first component
var curry = require('core.lambda').curry
var xss = [[1, 2], [3, 1], [-2, 4]]
function compare(a, b) {
return a < b? -1
: a === b? 0
: /* a> b */ 1
}
function first(xs) {
return xs[0]
}
function sortBy(f, xs) {
return xs.slice().sort(f)
}
var compareC = curry(2, compare)
sortBy(upon(compareC, first), xss) // => [[-2, 4], [1, 2], [3, 1]]
|