Functional Programing With Js Cheatsheet - Progress Whitepaper PDF
Functional Programing With Js Cheatsheet - Progress Whitepaper PDF
Functional Programing With Js Cheatsheet - Progress Whitepaper PDF
Example
// Since the function is a single expression return and braces are not
needed.
const multiply = (x, y) => x * y;
console.log(multiply(5,10)) //50
© 2018 Progress. All Rights Reserved.
Progress / Kendo UI
Function Delegates
Function delegates encapsulate a method allowing functions to be composed or passed as data.
Example
const isZero = n => n === 0;
const a = [0,1,0,3,4,0];
console.log(a.filter(isZero).length); // 3
Statement
const getSalutation = function(hour) {
var salutation; // temp value
if (hour < 12) {
salutation = "Good Morning";
}
else {
salutation = "Good Afternoon"
}
return salutation; // mutated value
}
Expression
const getSalutation = (hour) => hour < 12 ?
"Good Morning" : "Good Afternoon";
© 2018 Progress. All Rights Reserved.
Progress / Kendo UI
Higher Order Functions
A function that accepts another function as a parameter, or returns another function.
Example
function mapConsecutive(values, fn) {
let result = [];
for(let i=0; i < values.length -1; i++) {
result.push(fn(values[i], values[i+1]));
}
return result;
}
Currying
Currying allows a function with multiple arguments to be translated into a sequence of functions. Curried
functions can be tailored to match the signature of another function.
Example
Progress / Kendo UI
// const weightsInKg = weightsInPounds.map(x => convertUnits('kg', 0.45460,
0)(x));
// with currying
const weightsInKg = weightsInPounds.map(poundsToKg);
// 2.27kg, 7.00kg, 4.46kg, 50.01kg
[].every(fn)
Checks if all elements in an array pass a test.
[].some(fn)
Checks if any of the elements in an array pass a test.
[].find(fn)
Returns the value of the first element in the array that passes a test.
[].filter(fn)
Creates an array filled with only the array elements that pass a test.
[].map(fn)
Creates a new array with the results of a function applied to every element in the array.
[].reduce(fn(accumulator, currentValue))
Executes a provided function for each value of the array (from left-to-right). Returns a single value, the accumulator.
Reverses the order of the elements in an array. Use the spread operator to avoid mutation. [...arr].reverse()
Progress / Kendo UI
Method Chaining
Method chains allow a series of functions to operate in succession to reach a final result. Method chains allow
function composition similar to a pipeline.
Example
let cart = [{name: "Drink", price: 3.12},
{name: "Steak", price: 45.15},
{ name: "Drink", price: 11.01}];
Pipelines
A pipeline allows for easy function composition when performing multiple operations on a variable. Since
JavaScript lacks a Pipeline operator, a design pattern can be used to accomplish the task.
Example
const pipe = functions => data => {
return functions.reduce(
(value, func) => func(value),
data
);
};
Progress / Kendo UI
Author
Ed Charbeneau
Ed is a Microsoft MVP and an internationally recognized
online influencer, speaker, writer, a Developer Advocate
for Progress, and expert on all things web development.
Ed enjoys geeking out to cool new tech, brainstorming
about future technology, and admiring great design.
Kendo UI allows you to quickly build eye-catching, high-quality, high-performance responsive web-based
apps integrated into your technology of choice (jQuery, Angular, React, or Vue). Kendo UI offers a large
library of popular components from sophisticated grids and charts to basic buttons and menus. Access 70+
customizable UI components to speed up your development time by up to 50%.
Try Kendo UI
Worldwide Headquarters
Progress and Kendo UI are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other
countries. Any other trademarks contained herein are the property of their respective owners.
© 2018 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.
Rev 2018/03 | RITM0016138
Progress / Kendo UI