TL;DR

|>

pipe

pipe

Intro

--enable-pipeline-operator

let result = exclaim(capitalize(doubleSay("hello"))); result //=> "Hello, hello!" let result = "hello" |> doubleSay |> capitalize |> exclaim; result //=> "Hello, hello!"

Why not add it to JavaScript as well?

What then?

const pipe = (...args) => args.reduce((prev, curr) => curr(prev))

const pipe = (...args) => args.reduce((prev, curr) => curr(prev)) const result = pipe("hello" , $ => $ + ", " + $ , $ => $[0].toUpperCase() + $.slice(1) , $ => $ + '!' ) console.log(result)

const

var

is

|>

pipe

Function.pipe

window.pipe

How is pipe better?

// pseudo-map of data source url to corresponding pipeline // imagine data fetched from each url has a different format // // the first step in the pipeline ingests that format, // the last spits out some common format const data_sources_to_pipelines = [ [source_1_url, [source_1_function_1, source_1_function_2]], [source_2_url, [source_2_function_1]], [source_3_url, [source_3_function_1, source_3_function_2, source_3_function_3]] ] // the common format could then be processed by a common pipeline, // which runs it through some more steps until it's fully baked const common_pipeline = [common_function_1, common_function_2, common_function_3] // you could then use something like this to obtain // an array of processed data from all the sources: const data_processed = data_sources_to_pipelines.map(([url, pipeline]) => pipe(fetch(url), ...pipeline, ...common_pipeline) )

How is pipe worse?

What can we do about these terrible drawbacks?

let result = "hello" |> doubleSay |> capitalize |> exclaim

let result = pipe("hello" , doubleSay , capitalize , exclaim )

pipe

pipe

pipe

ᐅ

|>

pipe

pipe

pipe(

)

[,]

[shift] + [|], [>]

pipe

pipe

Function

const { pipe } = Function

pipe

NO, YOU DIDN'T CONVINCE ME! I MUST HAVE THE OPERATOR!!!11!

pipe

Outro

pipe

BTW they're trying to add it to TypeScript too. I'd advise against, on the same grounds.

Appendix

A nice feature of a code editor would be if you could magically switch back and forth between code with intermediate named variables and pipelined version of the same code, e.g. by selecting the code and invoking an option.

pipe

const compose = (...args) => args.reduceRight((prev, curr) => curr(prev)) // yum! const result = compose(exclaim, capitalize, doubleSay, "hello") const logged = (fn) => (...args) => { console.log('calling', fn) return fn(...args) } const memoized = (fn) => { const cache = new WeakMap() return (arg) => { if (cache.has(arg)) return cache.get(arg) const result = fn(arg) cache.set(arg, result) return result } } const special_square = compose( logged, memoized, (a) => a * a )

pipe

const { pipe: ᐅ } = Function const { PI: π } = Math const double = (n) => n * 2; const increment = (n) => n + 1; const result = ᐅ(π , double , increment , double ) // -> 14.566370614359172

Thanks

Discussion