fn factorial<S>()
    where
        S: Scheduler + 'static,
    {
        let actual_output: Rc<RefCell<Vec<usize>>> = Rc::new(RefCell::new(Vec::with_capacity(100)));
        let actual_output_clone = actual_output.clone();

        let root = Root::build_with_scheduler::<_, S>(|circuit| {
            TraceMonitor::new_panic_on_error().attach(circuit, "monitor");

            let mut n: usize = 0;
            let source = circuit.add_source(Generator::new(move || {
                n += 1;
                n
            }));
            let fact = circuit
                .iterate_with_condition_and_scheduler::<_, _, S>(|child| {
                    let mut counter = 0;
                    let countdown = source.delta0(child).apply(move |parent_val| {
                        if *parent_val > 0 {
                            counter = *parent_val;
                        };
                        let res = counter;
                        counter -= 1;
                        res
                    });
                    let (z1_output, z1_feedback) = child.add_feedback_with_export(Z1::new(1));
                    let mul = child.add_binary_operator(
                        Apply2::new(|n1: &usize, n2: &usize| n1 * n2),
                        &countdown,
                        &z1_output.local,
                    );
                    z1_feedback.connect(&mul);
                    Ok((countdown.condition(|n| *n <= 1), z1_output.export))
                })
                .unwrap();
            circuit.add_sink(
                Inspect::new(move |n| actual_output_clone.borrow_mut().push(*n)),
                &fact,
            );
        })
        .unwrap();

        for _ in 1..10 {
            root.step().unwrap();
        }

        let mut expected_output: Vec<usize> = Vec::with_capacity(10);
        for i in 1..10 {
            expected_output.push(my_factorial(i));
        }
        assert_eq!(&expected_output, actual_output.borrow().deref());
    }
// Nested circuit.  The root circuit contains a source node that counts up from
// 1.  For each `n` output by the source node, the nested circuit computes
// factorial(n) using a `NestedSource` operator that counts from n down to
// `1` and a multiplier that multiplies the next count by the product
// computed so far (stored in z-1).
Nested circuit.  The root circuit contains a source node that counts up from
1.
Nested circuit . The root circuit contains a source node that counts up from 1 .
fn factorial<S>()
    where
        S: Scheduler + 'static,
    {
        let actual_output: Rc<RefCell<Vec<usize>>> = Rc::new(RefCell::new(Vec::with_capacity(100)));
        let actual_output_clone = actual_output.clone();
        let root = Root::build_with_scheduler::<_, S>(|circuit| {
            TraceMonitor::new_panic_on_error().attach(circuit, "monitor");
            let mut n: usize = 0;
            let source = circuit.add_source(Generator::new(move || {
                n += 1;
                n
            }));
            let fact = circuit
                .iterate_with_condition_and_scheduler::<_, _, S>(|child| {
                    let mut counter = 0;
                    let countdown = source.delta0(child).apply(move |parent_val| {
                        if *parent_val > 0 {
                            counter = *parent_val;
                        };
                        let res = counter;
                        counter -= 1;
                        res
                    });
                    let (z1_output, z1_feedback) = child.add_feedback_with_export(Z1::new(1));
                    let mul = child.add_binary_operator(
                        Apply2::new(|n1: &usize, n2: &usize| n1 * n2),
                        &countdown,
                        &z1_output.local,
                    );
                    z1_feedback.connect(&mul);
                    Ok((countdown.condition(|n| *n <= 1), z1_output.export))
                })
                .unwrap();
            circuit.add_sink(
                Inspect::new(move |n| actual_output_clone.borrow_mut().push(*n)),
                &fact,
            );
        })
        .unwrap();
        for _ in 1..10 {
            root.step().unwrap();
        }
        let mut expected_output: Vec<usize> = Vec::with_capacity(10);
        for i in 1..10 {
            expected_output.push(my_factorial(i));
        }
        assert_eq!(&expected_output, actual_output.borrow().deref());
    }
fn factorial < S > ( ) where S : Scheduler + ' static , { let actual_output : Rc < RefCell < Vec < usize > > > = Rc :: new ( RefCell :: new ( Vec :: with_capacity ( 100 ) ) ) ; let actual_output_clone = actual_output . clone ( ) ; let root = Root :: build_with_scheduler :: < _ , S > ( | circuit | { TraceMonitor :: new_panic_on_error ( ) . attach ( circuit , " " ) ; let mut n : usize = 0 ; let source = circuit . add_source ( Generator :: new ( move | | { n += 1 ; n } ) ) ; let fact = circuit . iterate_with_condition_and_scheduler :: < _ , _ , S > ( | child | { let mut counter = 0 ; let countdown = source . delta0 ( child ) . apply ( move | parent_val | { if * parent_val > 0 { counter = * parent_val ; } ; let res = counter ; counter -= 1 ; res } ) ; let ( z1_output , z1_feedback ) = child . add_feedback_with_export ( Z1 :: new ( 1 ) ) ; let mul = child . add_binary_operator ( Apply2 :: new ( | n1 : & usize , n2 : & usize | n1 * n2 ) , & countdown , & z1_output . local , ) ; z1_feedback . connect ( & mul ) ; Ok ( ( countdown . condition ( | n | * n <= 1 ) , z1_output . export ) ) } ) . unwrap ( ) ; circuit . add_sink ( Inspect :: new ( move | n | actual_output_clone . borrow_mut ( ) . push ( * n ) ) , & fact , ) ; } ) . unwrap ( ) ; for _ in 1 .. 10 { root . step ( ) . unwrap ( ) ; } let mut expected_output : Vec < usize > = Vec :: with_capacity ( 10 ) ; for i in 1 .. 10 { expected_output . push ( my_factorial ( i ) ) ; } assert_eq ! ( expected_output actual_output borrow ( ) deref ( ) ) ; }
Nested circuit.
Nested circuit .