Crate cfrp [] [src]

Concurrent Function Reactive Programming

Highly influenced by Elm - provides a framework for describing & executing concurrent data flow processes.

Examples

use std::sync::mpsc::*;
use cfrp::*;

// create some channels for communicating with the topology
let (in_tx, in_rx) = channel();
//let (out_tx, out_rx) = channel();
 
// Topologies are statically defined, run-once structures.  Due to how
// concurrency is handled, changes to the graph structure can cause
// inconsistencies in the data processing
// 
// You can pass some state in (here we're passing `(in_rx, out_rx)`) if you need
// to.
spawn_topology( in_rx, |t, in_rx| {
 
    // Create a listener on `in_rx`.  Messages received on the channel will be
    // sent to any nodes subscribed to `input`
    let input = t.add(t.listen(in_rx));
 
    // Basic map operation.  Since this is a pure function, it will only be
    // evaluated when the value of `input` changes
    let plus_one = t.add(input.lift(|i| -> usize { i + 1 }));
 
    // The return value of `add` implements `Clone`, and can be used to
    // 'fan-out' data
    let plus_two = plus_one.clone().lift(|i| -> usize { i + 2 });
 
    // We can combine signals too.  Since it's possible to receive input on one
    // side but not the other, `lift2` always passes `Option<T>` to its
    // function.  Like `lift`, this function is only called when needed
    let combined = plus_one.lift2(plus_two, |i, j| -> usize {
        println!("lifting");
        match (i, j) {
            (Some(a), Some(b)) => a + b,
            _ => 0,
        } 
    });
 
    // `fold` allows us to track state across events.  Since this is assumed to
    // be impure, it is called any time a signal is received upstream of
    // `combined`.
    let accumulated = combined.fold(0, |sum, i| { *sum += i; });
 
    // Make sure to add transformations to the topology - if it's not added it
    // won't be run...
    t.add(accumulated);
});

in_tx.send(1usize).unwrap();
// let out: usize = out_rx.recv().unwrap();
// assert_eq!(out, 2);

Modules

primitives

Enums

Event

Container for data as it flows across the topology

Traits

Fold

Apply a function F which uses a data source Signal<A> to mutate an instance of B, generating an output data source Signal<B> containing the mutated value

Lift

Apply a pure function F to a data source Signal<A>, generating a transformed output data source Signal<B>.

Lift2

Apply a pure function F to a two data sources Signal<A>, and Signal<B>, generating a transformed output data source Signal<C>.

Push

Types which can receive incoming data from other signals

Signal

Types which can serve as a data source

Functions

spawn_topology

Construct a new topology and run it