neal 0.1.0-alpha.1

Simple, naive incremental dependency graphs. Don't use this. Not done.
Documentation
//! Minimal example showing the new observer-based API
//!
//! This demonstrates:
//! - Creating graphs with observers
//! - Dynamic node creation
//! - Automatic change notifications

use neal::{DerivedNode, Graph, GraphError, InputNode, Operation};
use std::hash::Hash;

#[derive(Clone, PartialEq, Hash)]
struct Number(i32);

// Operations for our Number type
struct Add;
impl Operation<(Number, Number), Number> for Add {
    fn execute(&self, inputs: &[&dyn neal::Value]) -> Result<Number, GraphError> {
        let a = inputs[0]
            .as_any()
            .downcast_ref::<Number>()
            .ok_or_else(|| GraphError::InvalidOperation("Expected Number".into()))?;
        let b = inputs[1]
            .as_any()
            .downcast_ref::<Number>()
            .ok_or_else(|| GraphError::InvalidOperation("Expected Number".into()))?;
        Ok(Number(a.0 + b.0))
    }

    fn input_count(&self) -> Option<usize> {
        Some(2)
    }
}

struct Multiply;
impl Operation<(Number, Number), Number> for Multiply {
    fn execute(&self, inputs: &[&dyn neal::Value]) -> Result<Number, GraphError> {
        let a = inputs[0]
            .as_any()
            .downcast_ref::<Number>()
            .ok_or_else(|| GraphError::InvalidOperation("Expected Number".into()))?;
        let b = inputs[1]
            .as_any()
            .downcast_ref::<Number>()
            .ok_or_else(|| GraphError::InvalidOperation("Expected Number".into()))?;
        Ok(Number(a.0 * b.0))
    }

    fn input_count(&self) -> Option<usize> {
        Some(2)
    }
}

fn main() -> Result<(), GraphError> {
    let mut graph = Graph::new();

    // Build a simple computation: (5 + 3) * 2 using new API
    let a = graph.add_node(InputNode::new(Number(5)));
    let b = graph.add_node(InputNode::new(Number(3)));
    let c = graph.add_node(InputNode::new(Number(2)));

    let sum = graph.add_node(DerivedNode::new(vec![a.id(), b.id()], Add));

    let result = graph.add_node(DerivedNode::new(vec![sum.id(), c.id()], Multiply));

    // Compute
    graph.compute()?;

    if let Some(value) = result.get_value(&graph) {
        println!("(5 + 3) * 2 = {}", value.0);
    }

    // Update and recompute
    a.set_value(&mut graph, Number(10))?;
    graph.compute()?;

    if let Some(value) = result.get_value(&graph) {
        println!("(10 + 3) * 2 = {}", value.0);
    }

    Ok(())
}