MithrilJS / mithril.js

A JavaScript Framework for Building Brilliant Applications
https://mithril.js.org
MIT License
13.98k stars 925 forks source link

Add lift to mithril/stream #1944

Closed spacejack closed 3 days ago

spacejack commented 7 years ago

Mithril currently ships a barebones streams library, which serves both to migrate code using the old m.prop and to add reactive programming features for working with async data.

Currently it supports several of the more commonly used fantasy land features (map, merge, combine.) combine, while useful, tends to be a bit cumbersome to use due to sending streams rather than values to the combiner callback.

I propose that we add stream.lift, which in most cases is preferable to stream.combine. It will work similarly to combine, however the callback will receive unwrapped values rather than streams. Here's the difference:

// using combine
c = stream.combine((a, b) => a() + b(), [astream, bstream])

// using lift
c = stream.lift((a, b) => a + b, astream, bstream)

(Note that both signatures match flyd's.)

The implementation should be quite small:

// ES6
function lift(f, ...streams) {
    return stream.merge(streams).map(s => f(...streams))
}

// Expanded to ES5
function lift() {
    var f = arguments[0]
    var streams = Array.prototype.slice.call(arguments, 1)
    return m.stream.merge(streams).map(function(s) {
        return f.apply(undefined, streams)
    })
}

Here is a flems example to play with.

tivac commented 7 years ago

I'm fine with this.

dead-claudia commented 3 days ago

Was added in #1950 and this was just not closed.