Hacker News new | ask | show | jobs
by sethcalebweeks 256 days ago
I love the idea! The creativity of (ab)using JavaScript type coersion is really neat. I did something similar using proxies to create a chainable API.

https://dev.to/sethcalebweeks/fluent-api-for-piping-standalo...

  const shuffle = (arr) => arr.sort(() => Math.random() - 0.5);
  const zipWith = (a, b, fn) => a.slice(0, Math.min(a.length, b.length)).map((x, i) => fn(x, b[i]));
  const log = (arr) => {
    console.log(arr);
    return arr;
  };

  const chain = chainWith({shuffle, zipWith, log});

  chain([1, 2, 3, 4, 5, 6, 7, 8, 9])
    .map((i) => i + 10)
    .log() // [ 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
    .shuffle()
    .log() // e.g. [ 16, 15, 11, 19, 12, 13, 18, 14, 17 ]
    .zipWith(["a", "b", "c", "d", "e"], (a, b) => a + b)
    .log() // e.g. [ '16a', '15b', '11c', '19d', '12e' ]
    [0]; // e.g. '16a'
1 comments

In another comment, I mentioned a vanilla JavaScript function I published in 2024 called Chute. https://github.com/gregabbott/chute

In a similar way to the featured project, Chute also uses proxies to work like a pipeline operator. But like in your reply, Chute uses a dot-notation style to chain and send data through a mix of functions and methods.

You might like to see how Chute uses proxies, as it requires no `chainWith` or similar setup step before use. Without setup, Chute can send data through global or local, top-level or nested, native or custom, unary, curried or non-unary functions and methods. It gives non-unary functions the current data at a specific argument position by using a custom-nameable placeholder variable.

The Chute page describes some more of its features: https://gregabbott.pages.dev/chute/