Functional programming with javascript

It's no secret: javascript offers many built-in constructs that make functional programming (or FP) easy and fun. I'll apply some of these to calculate derivatives of mathematical functions!

Derivatives

As we learned in calculus class many years ago, the definition of a derivative is
(f(x+h) - f(x)) / h
as h approaches 0, for the function f evaluated at x. In javascript, we can write this as
function derive(f, h) {
  function fprime(x) {
    return (f(x + h) - f(x)) / h;
  }
  return fprime;
}
where `fprime(x)` is the derivative of `f`. This demonstrates three aspects of FP:
  1. higher-order functions. `derive` takes a function `f` as input, and returns a function `fprime` as output
  2. closures. The definition of `fprime` uses, or "closes over", the values of `f` and `h`
  3. first-class functions. Within the body of `derive`, we are able to define a new function (`fprime`). Javascript allows us to treat functions as first-class citizens.

Here's an application of FP to calculate numeric approximations of derivatives. You pick a function, and it will calculate the y-values of the function and its 1st, 2nd, and 3rd derivatives over a series of x-values. Enjoy!
function
h
x min
x max
number of data points
refresh data