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 | // Sorting an array of pairs by the first component
var xss = [[1, 2], [3, 1], [-2, 4]]
function compare(a, b) {
return a < b? -1
: a === b? 0
: /* a> b */ 1
}
function sortBy(f, xs) {
return xs.slice().sort(f)
}
sortBy(upon(compare, first), xs) // => [[-2, 4], [1, 2], [3, 1]]
|