Help us understand the problem. What is going on with this article?

Functional Programingを破壊するBacon.fromArray()

More than 5 years have passed since last update.

FRPなjsライブラリのBacon.jsが提供するAPIの1つBacon.fromArray(values)があまりにも恐ろしい挙動をする。

恐ろしい挙動の例(CoffeeScript)。

sample.coffee
input = Bacon.fromArray([1..4])
input.onValue((n) -> console.log('1st onValue: '+n))
input.onValue((n) -> console.log('2nd onValue: '+n))
実行結果
1st onValue: 1 
1st onValue: 2 
1st onValue: 3 
1st onValue: 4 

素知らぬ顔で2nd onValueを無視する。

もっとやっかいな例。

sample2.coffee
input = Bacon.fromArray([1..4])
input2 = input.map((n) -> n+10)
input2.onValue((n) -> console.log('input2: '+n))
input.onValue((n) -> console.log('input: '+n))
実行結果
input2: 11 
input2: 12 
input2: 13 
input2: 14 

inputを直接いじってないのに、input.onValue()が無視される。

このキモい挙動の理由は、once(value)fromArray(values)によって作られたストリームは、onValue()とかlog()が呼ばれた時点で破壊的代入をされ、すべての要素が消去される仕様であるため。
"Functional" Reactive Programmingをするなら使わないほうがいい。

配列から新しいストリームを作りたければBacon.sequentially(0,values)を使えばキモくない。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away