Published
Edited
Dec 5, 2020
45 forks
1 star
Insert cell
Insert cell
Insert cell
Insert cell
function pipeline(...functions) {
// TODO code goes here
return function(input) {
// TODO more code goes here
};
}
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
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) {
// TODO
}
Insert cell
// Takes an ["array", "of", "strings"] and returns a camelized ["array", "Of", "Strings"]
function camelize(stringArray) {
// TODO
}
Insert cell
// Use additional cells ...
Insert cell
// ... to define ....
Insert cell
// ... more functions!
Insert cell
// Takes a "snake_case_string" and returns a "camelCaseString"
function snakeToCamel(snake_case_string) {
// TODO
}
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"
function snakeToTrain(snake_case_string) {
// TODO
}
Insert cell
Insert cell
// Insert additional cells with the "+" button if you need them!
Insert cell
md`-----

## [=> Solutions](https://observablehq.com/@anjana/solutions-function-composition)

_Just one possible way to solve this exercise, not the only way!_
`
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