Skip to content

Instantly share code, notes, and snippets.

@delonnewman
Last active August 10, 2018 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delonnewman/7e454d49309c856fdde8b8a980d6bb94 to your computer and use it in GitHub Desktop.
Save delonnewman/7e454d49309c856fdde8b8a980d6bb94 to your computer and use it in GitHub Desktop.
An Introduction to Abstraction with Functions
function square(x) {
return x * x;
}
function area(r) {
return Math.PI * square(r);
}
function add1(x) {
return 1 + x;
}
function fib(n) {
if (n <= 0) return 0;
else if (n === 1) return 1;
else {
return fib(n - 1) + fib(n - 2);
}
}
function add(a, b) {
return a + b;
}
[1, 2, 3].map(add1); // => [2, 3, 4]
[1, 2, 3].map(square); // => [1, 4, 9]
[1, 2, 3].map(fib); // => [1, 1, 2]
[1, 2, 3].reduce(add); // => 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment