bacon.js

Fighting with FRP

拈花ヽ惹草 提交于 2019-12-06 01:31:02
问题 I've read about FRP and was very excited. It looks great, so you can write more high-level code, and everything is more composable, and etc. Then I've tried to rewrite my own little game with a few hundreds sloc from plain js to Bacon. And I found that instead of writing high-level logic-only code, I actually beating with Bacon.js and its adherence to principles. I run into some headache that mostly interfere clean code .take(1) Instead of getting value, I should create ugly constructions.

Functional reactive operator for custom filter based on another Observable

淺唱寂寞╮ 提交于 2019-12-05 19:50:27
For fun and to learn I'm trying to implement an undo system in my app using functional reactive programming. I have a stream of state changes, which need to be saved onto the undo stack. When the user clicks undo, I take a value from the stack and update application state accordingly. The problem is that this update itself also generates an event in the state change stream. So what I would like is to derive another stream from state changes, which ommits state change right after undo. A simple diagram: states ----S----S----S---- undos -------U----------- save ----S---------S---- The first line

Bacon.js Maximum Call Stack Exceeded

拈花ヽ惹草 提交于 2019-12-05 17:27:06
I'm trying to produce a stream similar to Bacon.fromPoll for requestAnimationFrame Why does the following code produce a " Maximum call stack exceeded " error? function rafSequence() { var raf = Bacon.fromCallback(function(callback) { requestAnimationFrame(function() { callback(Date.now()); }); }); return raf.merge(raf.flatMap(rafSequence)); } rafSequence().log(); I thought merge() would garbage collect when one of the 2 streams threw a Bacon.End (the raf in raf.merge(...) . So why does it error? UPDATE: I have been able to implement a working version as follows: Bacon.repeat(() => Bacon

Does Functional Reactive Programming in JavaScript cause bigger problems with listener references?

夙愿已清 提交于 2019-12-04 10:05:40
问题 In JavaScript the observer pattern is used quite often. There is one tricky thing with it and that's the references the subject keeps of the observers. They require cleanup. For regular applications I use the following rules of thumb: If the subject has a life span shorter than (or equal to) the observer, I can just do subject.on('event', ...) If the subject has a life span longer than the observer, I need to use observer.listenTo(subject, 'event', ...) In the second case, the listenTo is

Fighting with FRP

淺唱寂寞╮ 提交于 2019-12-04 07:50:34
I've read about FRP and was very excited. It looks great, so you can write more high-level code, and everything is more composable, and etc. Then I've tried to rewrite my own little game with a few hundreds sloc from plain js to Bacon. And I found that instead of writing high-level logic-only code, I actually beating with Bacon.js and its adherence to principles. I run into some headache that mostly interfere clean code .take(1) Instead of getting value, I should create ugly constructions. Circular dependencies Sometimes they should be by logic. But implementing it in FRP is scary Active state

How to interleave streams (with backpressure)

江枫思渺然 提交于 2019-11-30 12:42:32
Suppose I have two possibly infinite streams: s1 = a..b..c..d..e... s2 = 1.2.3.4.5.6.7... I want to merge the streams and then map merged stream with slowish asynchronous operation (e.g. in Bacon with fromPromise and flatMapConcat ). I can combine them with merge : me = a12b3.c45d6.7e... And then map s1 = a..b..c..d..e... s2 = 1.2.3.4.5.6.7... me = a12b3.c45d6.7e... mm = a..1..2..b..3..c..4..5.. As you see greedier s2 streams gets advantage in the long run. This is undesired behaviour . The merge behaviour is not ok, as I want to have some kind of backpressure to have more interleaved, "fair",

Javascript: what does function(_) mean

纵饮孤独 提交于 2019-11-30 08:22:37
问题 I'm going through the bacon.js slide at: http://raimohanska.github.io/bacon.js-slides/1.html In the 1st line of the 2nd block, it says: function always(value) { return function(_) { return value } } what does function(_) mean? 回答1: In this case _ is just a function parameter - a single underscore is a convention used by some programmers to indicate "ignore this binding/parameter". Since JavaScript doesn't do parameter-count checking the parameter could have been omitted entirely. Such a

Node.js Streams vs. Observables

自古美人都是妖i 提交于 2019-11-29 18:56:25
After learning about Observables , I find them quite similar to Node.js streams . Both have a mechanism of notifying the consumer whenever new data arrives, an error occurs or there is no more data (EOF). I would love to learn about the conceptual/functional differences between the two. Thanks! m4ktub Both Observables and node.js's Streams allow you to solve the same underlying problem: asynchronously process a sequence of values. The main difference between the two, I believe, is related to the context that motivated its appearance. That context is reflected in the terminology and API. On the

Advantage of Functional Reactive Programming over event-listeners

心不动则不痛 提交于 2019-11-29 10:28:06
I've been hearing a lot about functional reactive programming, and decided to check out what the big deal is. Going through the bacon.js documentation, it seems that the main difference is that instead of setting an event listener on a component, I create an event stream on it, and pass the event handler into the stream instead. In other words, all I really did was move the event handler from the component to the event stream. Is that it? If so, what's the big advantage of doing this? Bergi Is that it? No. It's about having event streams. You still will attach listener to them in the end to

Javascript: what does function(_) mean

雨燕双飞 提交于 2019-11-29 06:46:36
I'm going through the bacon.js slide at: http://raimohanska.github.io/bacon.js-slides/1.html In the 1st line of the 2nd block, it says: function always(value) { return function(_) { return value } } what does function(_) mean? In this case _ is just a function parameter - a single underscore is a convention used by some programmers to indicate "ignore this binding/parameter". Since JavaScript doesn't do parameter-count checking the parameter could have been omitted entirely. Such a "throw-away" identifier is found more commonly in other languages, but consider a case like arr.forEach(function