Coder Social home page Coder Social logo

carboxyl's Introduction

Stories in Ready Build Status

Carboxyl is a library for functional reactive programming in Rust, a functional and composable approach to handle events in interactive applications. Read more in the docs…

Usage example

Here is a simple example of how you can use the primitives provided by Carboxyl. First of all, events can be sent into a sink. From a sink one can create a stream of events. Streams can also be filtered, mapped and merged. A signal is an abstraction of a value that may change over time. One can e.g. hold the last event from a stream in a signal.

extern crate carboxyl;

fn main() {
    let sink = carboxyl::Sink::new();
    let stream = sink.stream();
    let signal = stream.hold(3);

    // The current value of the signal is initially 3
    assert_eq!(signal.sample(), 3);

    // When we fire an event, the signal get updated accordingly
    sink.send(5);
    assert_eq!(signal.sample(), 5);
}

One can also directly iterate over the stream instead of holding it in a signal:

extern crate carboxyl;

fn main() {
    let sink = carboxyl::Sink::new();
    let stream = sink.stream();

    let mut events = stream.events();
    sink.send(4);
    assert_eq!(events.next(), Some(4));
}

Streams and signals can be combined using various primitives. We can map a stream to another stream using a function:

extern crate carboxyl;

fn main() {
    let sink = carboxyl::Sink::new();
    let stream = sink.stream();

    let squares = stream.map(|x| x * x).hold(0);
    sink.send(4);
    assert_eq!(squares.sample(), 16);
}

Or we can filter a stream to create a new one that only contains events that satisfy a certain predicate:

extern crate carboxyl;

fn main() {
    let sink = carboxyl::Sink::new();
    let stream = sink.stream();

    let negatives = stream.filter(|&x| x < 0).hold(0);

    // This won't arrive at the signal.
    sink.send(4);
    assert_eq!(negatives.sample(), 0);

    // But this will!
    sink.send(-3);
    assert_eq!(negatives.sample(), -3);
}

There are a couple of other primitives to compose streams and signals:

  • merge two streams of events of the same type.
  • Make a snapshot of a signal, whenever a stream fires an event.
  • lift! an ordinary function to a function on signals.
  • switch between different signals using a signal containing a signal.

See the documentation for details.

License

Copyright 2014, 2015, 2016 Eduard Bopp.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

carboxyl's People

Contributors

milibopp avatar moredread avatar killercup avatar ctsrc avatar llogiq avatar waffle-iron avatar tilpner avatar wolfiestyle avatar

Watchers

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.