Search code examples
javascriptfrpbacon.js

bacon.js - get value of stream/property


Probably it's simple question, but I can't resolve my problem. I have two streams and want mapping second stream by negative value of first stream.

jsfiddle - example

var price = change.map(1).scan(10, plus)
var money = buy.map(-price).merge(sale.map(price)).scan(100, plus);

Solution

  • This answer is basically what bergi said in the comments.

    var price = change.map(1).scan(10, plus)
    var purchasePrice = price.map(function(p) { return -p }).sampledBy(buy)
    var salePrice = price.sampledBy(sale)
    var money = purchasePrice.merge(salePrice).scan(100, plus)
    

    I used property.sampledBy(stream) instead of stream.map(property) - they do the same thing, but here I think it's clearer to use sampledBy.