Published
Edited
Jan 10, 2020
Insert cell
Insert cell
{
// Recursive definition of the Fibonacci series
// When calling this function, you must supply the zero-based index
// of the desired number in the sequence, followed by zero and one as
// the series starting values
var fib = (n,a,b) => n < 1 ? a : fib(n-1,b,a+b);

// Derive the 8th number in the Fibonacci series
return fib(8,0,1);
}
Insert cell
Insert cell
{
// Attempt 1: Try to extract the definition of fib by making it a
// parameter
var make_fib = fib => (n,a,b) => n < 1 ? a : fib(n-1,b,a+b);

// So, all I need to do is call make_fib and pass it the definition
// of fib as a parameter
var fib = make_fib(/* definition of fib */)
}
Insert cell
Insert cell
{
// Attempt 2: Calculate only the first number in the Fibonacci series
// Notice that the error function completely ignores its parameters.
// It does nothing more than return a constant value - which in this
// case is the string 'KABOOM'. This means that this function should
// only ever be called when an error condition has already occurred
var error = (n,a,b) => "KABOOM!";
var fib_next = fib_step => (n,a,b) => n < 1 ? a : fib_step(n-1,b,a+b);

// Create a function that gives us the first number in the Fibonacci
// series
var fib0 = fib_next(error); // -> (n,a,b) => n < 1 ? a : error(n-1,b,a+b)

return {
fib0 : fib0(0,0,1),
fib1 : fib0(1,0,1)
}
}
Insert cell
Insert cell
{
var error = (n,a,b) => "KABOOM!";
var fib_next = fib_step => (n,a,b) => n < 1 ? a : fib_step(n-1,b,a+b);
// Create a function that gives us the first number in the Fibonacci series
var fib0 = fib_next(error);
var fib1 = fib_next(fib_next(error));
var fib2 = fib_next(fib_next(fib_next(error)));
var fib3 = fib_next(fib_next(fib_next(fib_next(error))));

return {
fib0 : fib0(0,0,1),
fib0_1: fib0(1,0,1),
fib1 : fib1(1,0,1),
fib1_2: fib1(2,0,1),
fib2 : fib2(2,0,1),
fib2_3: fib2(3,0,1),
fib3 : fib3(3,0,1),
fib3_4: fib3(4,0,1)
}
}
Insert cell
Insert cell
{
var error = (n,a,b) => "KABOOM!";
var fib_next = fib_step => (n,a,b) => n < 1 ? a : fib_step(n-1,b,a+b);
// Create a function that gives us the first number in the Fibonacci series
var fib0 = fib_next(error);
var fib1 = fib_next(fib0);
var fib2 = fib_next(fib1);
var fib3 = fib_next(fib2);
var fib4 = fib_next(fib3);
var fib5 = fib_next(fib4);
var fib6 = fib_next(fib5);
var fib7 = fib_next(fib6);
var fib8 = fib_next(fib7);
var fib9 = fib_next(fib8);
// Function fib9 can now calculate any number in the Fibonacci series
// up to and including the 9th number. Trying to calculate the 10th
// number will cause an error.
return {
fib9_7 : fib9(7,0,1),
fib9_8 : fib9(8,0,1),
fib9_9 : fib9(9,0,1),
fib9_10: fib9(10,0,1)
}
}
Insert cell
Insert cell
{
// Attempt 3.1: Perform function wrapping on function fib_next. Here
// we pass the wrapper function the definition of fib_next and then
// call it immediately
var error = (n,a,b) => "KABOOM!";
var fib_x = (fib_next => fib_next(fib_next(error)))
(fib_step => (n,a,b) => n < 1 ? a : fib_step(n-1,b,a+b));
}
Insert cell
Insert cell
{
// Attempt 3.2: Perform function wrapping on function fib_next. Here
// we pass the wrapper function the inline definition of fib_next and
// then call it immediately
var error = (n,a,b) => "KABOOM!";
var fib_x = (fib_next => fib_next(fib_next(error)))
(fib_step => (n,a,b) => n < 1 ? a : fib_step(n-1,b,a+b));

return {
fib_x0: fib_x(0,0,1),
fib_x1: fib_x(1,0,1),
fib_x2: fib_x(2,0,1)
}
}
Insert cell
Insert cell
{
// Attempt 4.1: Remove the error function
//var error = (n,a,b) => console.log("KABOOM!");
//var fib_x = (fib_next => fib_next(fib_next(error)))
// (fib_step => (n,a,b) => n < 1 ? a : fib_step(n-1,b,a+b));

// This leaves us with a new definition of fib_x in which the error
// function is missing
var fib_x = (fib_next => fib_next(fib_next))
(fib_step => (n,a,b) => n < 1 ? a : fib_step(n-1,b,a+b));

return {
fib_x0 : fib_x(0,0,1),
fib_x1 : fib_x(1,0,1),
fib_x2 : fib_x(2,0,1)
}
}
Insert cell
Insert cell
{
// Attempt 4.2:
// Pass fib_step a copy of itself. Because fib_step returns a partial,
// we can immediately pass this partial the numbers it requires to
// perform the calculation we want
var fib_x = (fib_next => fib_next(fib_next))
(fib_step => (n,a,b) => n < 1 ? a : fib_step(fib_step)(n-1,b,a+b));
}
Insert cell
{
// Now rename all instances of fib_step to fib_next
var fib_x = (fib_next => fib_next(fib_next))
(fib_next => (n,a,b) => n < 1 ? a : fib_next(fib_next)(n-1,b,a+b));
}
Insert cell
Insert cell
{
var fib_x = (fib_next => fib_next(fib_next))
(fib_next => (n,a,b) => n < 1 ? a : fib_next(fib_next)(n-1,b,a+b));

// Let's try calculating different numbers in the Fibonacci series...
return {
fib0 : fib_x(0,0,1),
fib1 : fib_x(1,0,1),
fib2 : fib_x(2,0,1),
fib5 : fib_x(5,0,1),
fib20 : fib_x(20,0,1),
fib50 : fib_x(50,0,1),
fib100 : fib_x(100,0,1),
fib1474 : fib_x(1476,0,1)
}
}
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