Live data from Hacker News

Show HN: A JavaScript function that looks and behaves like a pipe operator

github.com

61–70 of 148 posts

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#61

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

Not sure if lodash is still in fashion but I use their flow and fp_ functions often. I like the readability, and tree shaking still works.

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#62

This 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!

Author here. Thanks! Yes I have to confess I have both a Scheme and Elm background ;)

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#63
Reminds me of something I tried hacking together in python for fun

  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()
  # False

Re: 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) => B

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#65

The 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

#67
post #22

Earlier 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

It feels a bit unnatural to pipe multiple arguments like that in JS, and you could inline them in the first call without losing legibility:

    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 x

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#69

How'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

The pipe function of `fp-ts` doesn’t have direct access to methods and properties of the pipe output like Verticalize. You have to wrap them into anonymous functions. Same with promises.

Re: Show HN: A JavaScript function that looks and behaves like a pipe operator

#70

The 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.

Agreed.
Post reply on HN