This is a cheat sheet describing the basics of FP in JS
|
Functional programming is a declarative paradigm of building software by composing pure functions.
|
|
It helps us to solve problems effectively in a simpler way
|
|
It improves modularity
|
|
It reduces complex problems into simple pieces
|
|
It helps us to debug the code quickly
|
|
Increment
|
const incr = x => x + 1
|
|
Add 2 numbers
|
const add = (x, y) => x + y
|
|
Square root
|
const squareRoot = x => Math.sqrt(x)
|
|
Square
|
const square = x => x * x
|
|
Add 2 numbers
|
const add = x => y => x + y
|
|
Multiply with a number
|
const multiply = m => n => n * m
|
|
Find volume
|
const volume = l => w => h => l w h
|
|
Find volume of cylinder
|
const aCylinder = volume(10)(10)(10)
|
|
Double each array element
|
const map1 = array1.map(x => x *2)
|
|
Print each array element
|
array1.forEach(e => console.log(e))
|
|
The sum of array elements
|
array1.reduce((acc, val) => acc + val)
|
|
Filter array
|
[1, 2, 3].filter(n => n > 2)
|
function greaterThan(n) {
return x => x > n;
}let greaterThanTwo = greaterThan(2);console.log(greaterThanTwo(5));
|
var compose = (f, g) => (x) => f(g(x));
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.