Frappuccino
Functional Reactive Programming for Ruby.
Installation
Add this line to your application's Gemfile:
gem 'frappuccino'
And then execute:
$ bundle
Or install it yourself as:
$ git clone https://github.com/steveklabnik/frappuccino
$ cd frappuccino
$ bundle
$ rake install
Usage
Basically, this:
require 'frappuccino'
class Button
def push
emit(:pushed)
end
end
button = Button.new
stream = Frappuccino::Stream.new(button)
counter = stream
.map {|event| event == :pushed ? 1 : 0 }
.inject(0) {|sum, n| sum + n }
counter.now
button.push
button.push
button.push
counter.now
button.push
counter.now
You can also map via a hash, if you prefer:
.map(:pushed => 1, :default => 0)
You can also register callbacks to a Stream. These will executed for
each event that occurs in the Stream:
stream.on_value do |event|
puts "I got a #{event}!"
end
You can combine two streams together:
merged_stream = stream_one.merge(stream_two)
merged_stream = Frappuccino::Stream.merge(one_stream , other_stream)
merged_stream = Frappuccino::Stream.new(button_one, button_two)
You can select events from a stream, too:
stream = Frappuccino::Stream.new(button, something_else)
filtered_stream = stream.select{|event| event == :pushed }
filtered_stream.on_value do |event|
end
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request