Published
Edited
Dec 4, 2020
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
function pipeline(...functions) {
if (length(functions) === 0) return input => input;
if (length(functions) === 1) return input => head(functions)(input);
return function(input) {
return pipeline(...tail(functions))(head(functions)(input));
};
}
Insert cell
Insert cell
pluralize = singularWord => singularWord + 's'
Insert cell
heart = word => "I ❤️ " + word
Insert cell
exclaim = sentence => sentence + "!"
Insert cell
Insert cell
showSomeLove = pipeline(pluralize, heart, exclaim);
Insert cell
pipelineLove = showSomeLove('pipeline')
Insert cell
Insert cell
functionLove = showSomeLove('pure function')
Insert cell
Insert cell
coffeeLove = showSomeLove('coffee break')
Insert cell
Insert cell
Insert cell
loveSomeShow = pipeline(exclaim, heart, pluralize)
Insert cell
wrongOrder = loveSomeShow('pipeline')
Insert cell
Insert cell
Insert cell
composedPipe = pipeline(pluralize, pipeline(heart, exclaim))
Insert cell
compositionLove = composedPipe('composition') // should be equivalent to: showSomeLove('composition')
Insert cell
Insert cell
composedPipe2 = pipeline(pipeline(pluralize, heart), exclaim)
Insert cell
compositionLove2 = composedPipe2('composition') // should be equivalent to: composedPipe('composition')
Insert cell
Insert cell
Insert cell
function reducePipeline(...functions) {
return input => reduce((acc, fn) => fn(acc), input, functions);
}
Insert cell
Insert cell
Insert cell
// Takes a "snake_case_string" and returns a split array of the words, e.g. ["snake", "case", "string"]
function desnake(snake_case_string) {
return snake_case_string.split('_');
}
Insert cell
// Takes a "string" and returns a string with the first letter capitalized, e.g. "String"
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.substr(1).toLowerCase();
}
Insert cell
// Takes an ["array", "of", "strings"] and returns a camelized ["array", "Of", "Strings"]
function capitalizeAll(stringArray) {
return map(capitalizeFirstLetter, stringArray);
}
Insert cell
function camelize(stringArray) {
return [head(stringArray)].concat(capitalizeAll(tail(stringArray)));
}
Insert cell
function concatenate(stringArray) {
return reduce((acc, str) => acc + str, '', stringArray);
}
Insert cell
function hyphenate(stringArray) {
return reduce(
(acc, str) => [acc, str].join('-'),
head(stringArray),
tail(stringArray)
);
}
Insert cell
// Takes a "snake_case_string" and returns a "camelCaseString"
function snakeToCamel(snake_case_string) {
const pipe = pipeline(desnake, camelize, concatenate);
return pipe(snake_case_string);
}
Insert cell
snakeToCamel("super_cool_variable")
Insert cell
Insert cell
snakeToCamel("very_long_variables_should_also_work")
Insert cell
Insert cell
snakeToCamel("edgecase")
Insert cell
Insert cell
Insert cell
// Takes a "snake_case_string" and returns a "Train-Case-String"
snakeToTrain = pipeline(desnake, capitalizeAll, hyphenate)
Insert cell
Insert cell
Insert cell
// Insert additional cells with the "+" button if you need them!
Insert cell
Insert cell
Insert cell
Insert cell
length([1, 2, 3])
Insert cell
head([1, 2, 3])
Insert cell
tail([1, 2, 3])
Insert cell
multiplied = reduce((acc, val) => acc * val, 1, [1, 2, 3])
Insert cell
Insert cell
add10 = map(val => val + 10, [1, 2, 3])
Insert cell
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