If you are interested in this kind of things, also have a look at fp-ts and it’s version of pipe and flow. Also comes with various monads to play around with! [1] https://github.com/gcanti/fp-ts
Show HN: A JavaScript function that looks and behaves like a pipe operator
61–70 of 148 posts
Re: Show HN: A JavaScript function that looks and behaves like a pipe operator
#62This is cool, I love JS syntax hacks. Well done! const { status } = await send(capitalize(greeting) + "!") console.log(status) I find that easy to read, and your pipe operator harder (but still sensible!). I guess that just reflects the backgrounds we come from, people from more functional backgrounds (maybe lisp or Haskell) will find the latter example easier I guess!
Re: Show HN: A JavaScript function that looks and behaves like a pipe operator
#63 from functools import partial
class Pipeable:
def __init__(self, fn):
self.fn = fn
def __ror__(self, lhs):
return self.fn(lhs)
def pipeable(fn):
return lambda *args: Pipeable(partial(fn, *args))
filter = pipeable(filter)
map = pipeable(map)
list = pipeable(list)
sum = pipeable(sum)
min = pipeable(min)
max = pipeable(max)
any = pipeable(any)
# Usage:
range(1, 100) | filter(lambda x: x = 5) | any()
# FalseRe: Show HN: A JavaScript function that looks and behaves like a pipe operator
#64 export const pipe = (f: Fun) => {
return {
to: (g: Fun) => pipe((arg: A) => g(f(arg))),
build: () => f,
};
};
Much simpler alternative. const process = pipe(readLines).to(x => cut(x, 2)).to(....).build()
const result = await process(fd)
P.S the Fun type is just (...args: A) => BRe: Show HN: A JavaScript function that looks and behaves like a pipe operator
#65The following example code is a bit hard to read: const { status } = await send(capitalize(greeting) + "!") console.log(status) I disagree, I find this example code very easy to read because it reads like idiomatic Javascript. Unlike this library.
The strength of the pipeline operators comes when you have to do method chaining things like: const foo = capitalize(underline(reverse(exclaim(indent(value))))); I already forgot the amount of parentheses I needed to close with while typing that.
value.indent()
.exclaim()
.reverse()
.underline()
.capitalize()
There’s always the OOP version of method chaining!Re: Show HN: A JavaScript function that looks and behaves like a pipe operator
#66Re: Show HN: A JavaScript function that looks and behaves like a pipe operator
#67Earlier quoted context omitted.
Which is basically what the proposal is https://github.com/tc39/proposal-pipeline-operator#this-prop...
The difference is that they want to turn a = d(c(b,7)) into a = b |> c(%,7) |> d(%) and I would like to see it turn into a = b,7 ~> c ~> d
a = c(b, 7) |> d(%)
It's the arguments added way down the line that are problematic: a = f((e(d(c(b))), 4), 5)
a = ((b ~> c ~> d), 4 ~> e), 5 ~> f
a = b |> c(%) |> d(%) |> e(%, 4) |> f(%, 5)
Smart pipes [1] were a bit nicer about that: a = b |> c |> d |> e(#, 4) |> f(#, 5)
Basically you would use # token whenever you wanted an expression and just a function name when you needed to pass a single argument. Best of both worlds IMO.Perhaps it could even be extended for Curry-ish function syntax (although it isn't as obvious):
a = b |> c(7) |> d
a = d(c(7, b))
Too bad it was withdrawn.[1]: https://github.com/tc39/proposal-smart-pipelines
P. S. When studying Haskell on a CS course in school, I used to define exactly the same squiggly arrow operator for my programs :-)
x ~> f = f xRe: Show HN: A JavaScript function that looks and behaves like a pipe operator
#68Re: Show HN: A JavaScript function that looks and behaves like a pipe operator
#69How's it different from other `pipe` implementations around, e.g. the one from `fp-ts`? https://gcanti.github.io/fp-ts/modules/function.ts.html#pipe
Re: Show HN: A JavaScript function that looks and behaves like a pipe operator
#70The following example code is a bit hard to read: const { status } = await send(capitalize(greeting) + "!") console.log(status) I disagree, I find this example code very easy to read because it reads like idiomatic Javascript. Unlike this library.