Object.prototype.pipe = function(fn) { return fn(this) }
'hello'.pipe(upper).pipe(ex('!!!'))
Or code golf version: Object.prototype.P=function(...F){return F.reduce((v,f)=>f(v),this)}
'hello'.P(upper,ex('!!!'))91–100 of 109 posts
Object.prototype.pipe = function(fn) { return fn(this) }
'hello'.pipe(upper).pipe(ex('!!!'))
Or code golf version: Object.prototype.P=function(...F){return F.reduce((v,f)=>f(v),this)}
'hello'.P(upper,ex('!!!'))First example doesn't work though: const greeting = pipe('hello') | upper | ex('!!!') await greeting.run() // → "HELLO!!!" If you look at the tests file, it needs to be written like this to make it work: let greeting; (greeting = pipe('hello')) | upper | ex('!!!'); await greeting.run(); Which is not anymore as ergonomic.
You’ve implied what I’ll state clearly:
Pipes are for composing transformations, one per line, so that reading comprehension doesn’t nosedive too fast with accumulation of subsequent operations.
Chaining on the same line is shit for readying and worst for git merges and PR reviews.
First example doesn't work though: const greeting = pipe('hello') | upper | ex('!!!') await greeting.run() // → "HELLO!!!" If you look at the tests file, it needs to be written like this to make it work: let greeting; (greeting = pipe('hello')) | upper | ex('!!!'); await greeting.run(); Which is not anymore as ergonomic.
I was playing with it, and you can do this, which looks a little better. const greeting = pipe('hello'); greeting | upper | ex('!!!'); await greeting.run(); // → "HELLO!!!" Since it uses the "Symbol.toPrimitive" method, you can use any operator (not just "bitwise OR" (|)). const greeting = pipe('hello'); greeting / upper * ex('!!!'); await greeting.run(); // → "HELLO!!!"
Mutating your inputs is not functional programming. And pipes are effectively compact list comprehensions. Comprehensions without FP is Frankensteinian.
Earlier quoted context omitted.
I think JS's map/reduce/filter design is one of the worst ones out there actually - map has footguns with its extra arguments and everything gets converted to an array at the drop of a hat. Still, pipeline syntax probably won't help fix any of that.
I always thought JS map filter reduce felt quite nice, especially playing around with data in the REPL. Java maps with all the conversions back and forth to streams are clumsy.
Earlier quoted context omitted.
`map` returns an array and can only be called on an array.
Right, but I’m not clear on what gets converted to an array. Do you mean more or less what I said in my previous comment? That it requires you (your code, or calling code in general) to perform that conversion excessively?
Further proof that JavaScript accidentally became the new C++. “Aren’t you surprised that this syntax works?” is not praise for a language design.
It's a clever hack. This is Hacker News. Let's try to appreciate clever hacks while here.
Piping syntax is nice for reading, but it's hard to debug. There's no clear way to "step through" each stage of the pipe to see the intermediate results.