4 ms·
In Python or Javascript, I look for opportunities to use pipe() or pipeeither() [1] wherever possible. These crop up a lot -- CLI scripts, data processing lamb
by bradwood 4y ago
In Python or Javascript, I look for opportunities to use pipe() or pipeeither() [1] wherever possible. These crop up a lot -- CLI scripts, data processing lambdas, etc.
A quick and dirty pipeither() with a "poor mans" either monad and you get a lot of the benefits of pure, testable functions, easy maintenance and debugging etc.
It's not Haskell, but its good.
[1]
const pipelineeither = function (l) {
const pipelineeither = function (v) {
let [left, right] = [null, v];
for (const f of l) {
if (left !== null) break;
[left, right] = f(right);
}
return [left, right];
};
return pipelineeither;
};
- bradwood 4y agoand in Python: def pipeeither(l): def pipeeither(v): return reduce(lambda a, e: e(a[1]) if a[0] is None else a, l, (None, v)) return pipeeither