3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

suinさんのじゃんけんをFRP風に書いてみた。

Last updated at Posted at 2013-03-27

元ネタ

RFPとは?

functional reactive programmingのことです。
詳細はこちら

どんな時に役立つの?

webとかでイベントがどんどん発火する時、簡単かつ清潔に対応するが出来ます。例えば

var plus = $("#plus").asEventStream("click").map(1);
var minus = $("#minus").asEventStream("click").map(-1);
var both = plus.merge(minus);

both.onValue(function(val) { /* val will be 1 or -1 */ });

jQueryセレクターから簡単にevent streamを生成して操作する事が可能になります。

Bacon.js

ブラウザ、Node.js両方とも使えるRFPライブラリです。

コード

beats関数は元ネタの物をそのまま使っています。

Bacon = require 'baconjs'

you = new Bacon.Bus()
opponent = new Bacon.Bus()

result = you.toProperty()
            .combine(opponent.toProperty(), beats)
            .onValue (d) -> console.log d

you.push 'paper' # you: paper
opponent.push 'paper' # you: paper, opponent: paper => false
opponent.push 'rock' # you: paper, opponent: rock => true
you.push 'scissors' # you: scissors, opponent: rock => false
opponent.push 'paper' # you: scissors, opponent: paper => true

コンセプトとしてはこんな感じになります。
概要

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?