EventStreams circular dependencies in FRP

All examples use Ramdahow _(it clearly shows what the methods do in the contexts of the examples) and kefirhow frp(almost the same API as in bacon.js)

I have a thread that describes a change in position.

var xDelta = frp
    .merge([
        up.map(_.multiply(1)),
        down.map(_.multiply(-1))
    ])
    .sampledBy(frp.interval(10, 0))
    .filter();

It emits +1when I press a key UP, and -1on DOWN.

To get position me scan, this delta

var x = xDelta
    .scan(_.add)
    .toProperty(0);

This works as expected. But I want to limit the value xfrom 0to 1000.

To solve this problem, I found two solutions:

  • Change function to scan

    var x = xDelta.scan(function (prev, next) {
        var newPosition = prev + next;
        if (newPosition < 0 && next < 0) {
            return prev;
        }
        if (newPosition > 1000 && next > 0) {
            return prev;
        }
        return newPosition;
    }, 0);
    

It looks good, but later, when new rules appear, this method will grow. Therefore, I mean that it does not look compositional and FRPy.

  1. current. delta. delta current, current after applying .

    • current delta
    • delta current after applying
    • current after applying current

    , . flatMap.

    var xDelta = frp
        .merge([
            up.map(_.multiply(1)),
            down.map(_.multiply(-1))
        ])
        .sampledBy(frp.interval(10, 0))
        .filter();
    
    var possibleNewPlace = xDelta
        .flatMap(function (delta) {
            return x
                .take(1)
                .map(_.add(delta));
        });
    
    var outOfLeftBoundFilter = possibleNewPlace
        .map(_.lte(0))
        .combine(xDelta.map(_.lte(0)), _.or);
    
    var outOfRightBoundFilter = possibleNewPlace
        .map(_.gte(1000))
        .combine(xDelta.map(_.gte(0)), _.or);
    
    var outOfBoundFilter = frp
        .combine([
            outOfLeftBoundFilter,
            outOfRightBoundFilter
        ], _.and);
    
    var x = xDelta
        .filterBy(outOfBoundFilter)
        .scan(_.add)
        .toProperty(0);
    

    iofjuupasli/capture-the-sheep-frp

    gh-pages

    , , , -.

FRP?

Controller Model, .

, :

              ---> Model
Controller ---|
              ---> Model

FRP Controller. Model Model. , Model1 Model2, , Model2 Model1?

Model ----->
      <----- Model

, : position movement. movement position .

. FRP . , - .

+4
2

, , -

. , , , . . , , , . . Haskell let x = x + 1 - .

current delta, delta current after applying, current after applying current

, . , current . .

current . FRP, stepper. :

e = ((+) <$> b) <@> einput
b = stepper 0 e

, <$> <@>, , , , e ( "" ) b einput. , :

e = ((+) <$> bound) <@> einput
bound = (min 0) <$> (max 1000) <$> b
b = stepper 0 e

, scan. , .

stepper JS FRP 1. Bacon Kefir , , Bus, . : -)

[1]: , , - ( ). stepper - , JavaScript .

+4

/ cyclejs, , , webfrontend Facebook .

, , , , , , , (DOM ) "", , .

, .

+1

All Articles