Argument Selection Function

The argument selection function λfirst.λsecond.first accepts a function as an argument and returns a function which, when given an argument, returns the first argument.

// λfirst.λsecond.first
const selectFirst = x => y => x

const double = a => a * 2

const triple = a => a * 3

const applyDouble = selectFirst(double)
const applyTriple = applyDouble(triple)

applyTriple(3) // equals 6

Here's the same example with the second argument selected:

// λfirst.λsecond.second
const selectSecond = x => y => y

const double = a => a * 2

const triple = a => a * 3

const applyDouble = selectSecond(double)
const applyTriple = applyDouble(triple)

applyTriple(3) // equals 9

Argument selection can be built upon for further composition, allowing us to pair arguments in different ways such as the following pair function λfirst.λsecond.λfunc.((func first) second), which applies the first argument to a provided function, and then the second argument to the result of that.

// λfirst.λsecond.λfunc.((func first) second)
const makePair = first => second => func => func(first)(second)

const double = a => a * 2

const triple = a => a * 3

const multiply = a => b => a * b

makePair(double(2))(triple(2))(multiply) // equals 24

The same in F# which is slighltly more concise:

// λfirst.λsecond.λfunc.((func first) second)
let makePair first second func = func first second

let double a = a * 2

let triple a = a * 3

let multiply a b = a * b

makePair (double 2) (triple 2) multiply // equals 24

Finally, some languages like F# have a function composition operator which make functions like the makePair one above brief and elegant.

let double a = a * 2

let triple a = a * 3

let multiply a b = a * b

let makePair = double >> triple >> multiply

makePair 2 2 // equals 24