Published
Edited
Jan 10, 2020
Insert cell
Insert cell
// An abstract function that stores a PAIR of values (val1 & val2), together with some function (fn) that
// decides whether the first or second value should be returned
PAIR = val1 => val2 => fn => fn(val1)(val2)
Insert cell
Insert cell
Insert cell
// The TAIL function arbitrarily returns the second item in a pair
TAIL = val1 => val2 => val2
Insert cell
Insert cell
colours = PAIR("sky_blue_pink")("cherry_green")
Insert cell
colours(HEAD)
Insert cell
colours(TAIL)
Insert cell
Insert cell
colour = "sky_blue_pink"
Insert cell
// Start with an imperative example
{
if (colour === "sky_blue_pink") {
return "Pigs fly"
}
else {
return "Elephants jump"
}
}
Insert cell
Insert cell
{
// Hide the condition inside a predicate function
var is_sbp = colour => colour === "sky_blue_pink"

if (is_sbp("sky_blue_pink"))
return "Pigs fly"
else
return "Elephants jump"
}
Insert cell
Insert cell
{
// Hide the condition inside a predicate function
var is_sbp = colour => colour === "sky_blue_pink"

// Use the JavaScript ternary operator
return is_sbp("sky_blue_pink") ? "Pigs fly" : "Elephants jump"
}

Insert cell
Insert cell
// If the predicate evaluates to true
true ? "Pigs fly" : "Elephants jump"
Insert cell
// If the predicate evaluates to false
false ? "Pigs fly" : "Elephants jump"
Insert cell
Insert cell
{
// Rewrite the is_sbp predicate function to return HEAD and TAIL
var is_sbp = colour => (colour === "sky_blue_pink") ? HEAD : TAIL
// Store the responses to the colour test as a PAIR
var colourResponses = PAIR("Pigs fly")("Elephants jump")
return colourResponses(is_sbp("sky_blue_pink"))
}
Insert cell
Insert cell
// Rename the HEAD and TAIL functions to TRUE and FALSE and give the
// parameters some user-friendly names. So
// var HEAD = val1 => val2 => val1;
// var TAIL = val1 => val2 => val2;
// becomes:
TRUE = true_part => false_part => true_part
Insert cell
FALSE = true_part => false_part => false_part
Insert cell
Insert cell
{
// Rewrite the is_sbp predicate function to return HEAD and TAIL
var is_sbp = colour => (colour === "sky_blue_pink") ? TRUE : FALSE
// Store the responses to the colour test as a PAIR
var colourResponses = PAIR("Pigs fly")("Elephants jump")
return colourResponses(is_sbp("sky_blue_pink"))
}
Insert cell
Insert cell
{
// The colour test function returns TRUE or FALSE
var is_sbp = colour => (colour === "sky_blue_pink") ? TRUE : FALSE

// Store the responses to the colour test as a PAIR
var colourResponses = PAIR("Pigs fly")("Elephants jump")

// Modify HEAD and TAIL to receive a PAIR
var HEAD = pair => pair(TRUE)
var TAIL = pair => pair(FALSE)

// Now HEAD and TAIL can be used in a more intuitive manner
return HEAD(colourResponses)
}
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more