Published
Edited
Jan 10, 2020
Insert cell
Insert cell
// Create a adder function
adder = value1 => value2 => value2 + value1
Insert cell
// Using the adder function, create an incr function
incr = adder(1)
Insert cell
// Create a multiplier function
multiplier = p => q => q * p
Insert cell
Insert cell
double = multiplier(2) // -> double = q => q * 2
Insert cell
triple = multiplier(3) // -> triple = q => q * 3
Insert cell
quadruple = multiplier(4) // -> quadruple = q => q * 4
Insert cell
double(21)
Insert cell
triple(9)
Insert cell
quadruple(18)
Insert cell
Insert cell
// Now this is getting silly (but then, this is JavaScript)
FIFTEEN = transform => start_value =>
transform(
transform(
transform(
transform(
transform(
transform(
transform(
transform(
transform(
transform(
transform(
transform(
transform(
transform(
transform(start_value)))))))))))))))
Insert cell
FIFTEEN(incr)(0)
Insert cell
Insert cell
ZERO = transform => start_value => start_value
Insert cell
SUCC = qtyFn => transform => start_value => transform(qtyFn(transform)(start_value))
Insert cell
Insert cell
ONE = SUCC(ZERO)
Insert cell
TWO = SUCC(ONE)
Insert cell
THREE = SUCC(TWO)
Insert cell
FOUR = SUCC(THREE)
Insert cell
FIVE = SUCC(FOUR)
Insert cell
SIX = SUCC(FIVE)
Insert cell
SEVEN = SUCC(SIX)
Insert cell
EIGHT = SUCC(SEVEN)
Insert cell
NINE = SUCC(EIGHT)
Insert cell
TEN = SUCC(NINE)
Insert cell
Insert cell
Insert cell
// Incrememt 0 seven times
SEVEN(incr)(0)
Insert cell
// Double 1 seven times
SEVEN(double)(1)
Insert cell
// Triple 1 seven times
SEVEN(triple)(1)
Insert cell
Insert cell
// Concatenate any character to a string
str_concatenate = char => acc => acc.concat(char)
Insert cell
// Add a plus character to a string
asPlusChar = str_concatenate('+')
Insert cell
// Reify an abstract quantity function as an integer
to_integer = qtyFn => qtyFn(incr)(0)
Insert cell
// Reify an abstract quantity function as a string
to_string = qtyFn => qtyFn(asPlusChar)('')
Insert cell
to_integer(SEVEN)
Insert cell
to_integer(SUCC(TWO))
Insert cell
to_string(FIVE)
Insert cell
to_string(SUCC(EIGHT))
Insert cell
Insert cell
ADD = qtyFn1 => qtyFn2 => qtyFn1(SUCC)(qtyFn2)
Insert cell
to_integer(ADD(TWO)(THREE))
Insert cell
to_integer(ADD(NINE)(EIGHT))
Insert cell
Insert cell
// Our transformation function
ADD(THREE)
Insert cell
// ADD(THREE) is now applied FOUR times to...?
FOUR(ADD(THREE))
Insert cell
Insert cell
// Add THREE to ZERO, FOUR times
FOUR(ADD(THREE))(ZERO)
Insert cell
to_integer(FOUR(ADD(THREE))(ZERO))
Insert cell
Insert cell
MULTIPLY = qtyFn1 => qtyFn2 => qtyFn1(ADD(qtyFn2))(ZERO)
Insert cell
// Three times four
to_integer(MULTIPLY(THREE)(FOUR))
Insert cell
// And just to prove that multiplication is commutative
to_integer(MULTIPLY(FOUR)(THREE))
Insert cell
Insert cell
// Create a POWER function
POWER = qtyFn1 => qtyFn2 => qtyFn2(MULTIPLY(qtyFn1))(ONE)
Insert cell
// Three raised to the power of four
to_integer(POWER(THREE)(FOUR))
Insert cell
// Exponentiation is not commutative
to_integer(POWER(FOUR)(THREE))
Insert cell
Insert cell
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