Code Monkey home page Code Monkey logo

frappuccino's Introduction

Frappuccino

Functional Reactive Programming for Ruby.

Build Status Code Climate Coverage Status

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) # emit sends a value into the stream
  end
end

button = Button.new
stream = Frappuccino::Stream.new(button)

counter = stream
            .map {|event| event == :pushed ? 1 : 0 } # convert events to ints
            .inject(0) {|sum, n| sum + n } # add them up

counter.now # => 0

button.push
button.push
button.push

counter.now # => 3

button.push

counter.now # => 4

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)

# or

merged_stream = Frappuccino::Stream.merge(one_stream , other_stream)

# or

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|
  # event will only ever be :pushed
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

frappuccino's People

Contributors

steveklabnik avatar seadowg avatar avgerin0s avatar sarahhodne avatar vipulnsward avatar brixen avatar craiglittle avatar envygeeks avatar larrylv avatar parndt avatar

Watchers

Nestor G Pestelos Jr avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.