paldepind / flyd

The minimalistic but powerful, modular, functional reactive programming library in JavaScript.
MIT License
1.56k stars 85 forks source link

provide pointfree-friendly `combine`? #158

Closed KiaraGrouwstra closed 7 years ago

KiaraGrouwstra commented 7 years ago

If I look at the combine examples in the readme, I felt its API to be a bit unfriendly in the sense that it requires the user to still manually 'apply' the streams to obtain their values, meaning one would not be able to just pass in an existing function. e.g. for flyd.combine((x, y) => x() + y(), [x, y]), I would prefer being able to write say flyd.combine(R.add, [x, y]).

I tried a PoC to achieve this at RunKit:

var R = require("ramda");
var flyd = require("flyd");

var combine2 = (f, streams) => flyd.combine(
    R.pipe(
        function() { return arguments; },
        R.dropLast(1),
        R.map((stream) => stream()),
        R.apply(f)
    ),
    streams
);

var x = flyd.stream(4);
var y = flyd.stream(6);
var sum = combine2(R.add, [x, y]);
x(12);
console.log(sum()); // 18
y(8);
console.log(sum()); // 20

I've no idea if adding an API like this (under whatever name) is considered desirable here, but this was a point that irked me as an outsider. It's admittedly optimized for developer experience rather than for say performance.

c-dante commented 7 years ago

Combine is mostly a low level combination function.

You can just use module/lift to achieve the combine style you're after.

KiaraGrouwstra commented 7 years ago

Cool, thanks. :)