Module composable

Module composable 

Source
Expand description

Composable operations for keypath functions

This module provides composable operations that allow chaining keypath transformations in a functional programming style. It includes pipe operations, conditional operations, and lazy evaluation patterns.

§Examples

§Basic Composition

use rust_prelude_plus::prelude::*;
use key_paths_derive::Keypath;
use std::rc::Rc;

#[derive(Keypath, Debug, Clone)]
struct Person {
    name: String,
    age: u32,
}

let people = vec![
    Rc::new(Person { name: "Alice".to_string(), age: 30 }),
    Rc::new(Person { name: "Bob".to_string(), age: 25 }),
];

// Compose operations using pipe
let result = pipe(people, |people| {
    people.iter()
        .filter_by_keypath(Person::age(), |&age| age < 30)
        .map_keypath(Person::name(), |name| name.clone())
        .collect::<Vec<_>>()
});

§Conditional Operations

use rust_prelude_plus::prelude::*;
use key_paths_derive::Keypath;
use std::rc::Rc;

#[derive(Keypath, Debug, Clone)]
struct Product {
    name: String,
    price: f64,
    category: String,
}

let products = vec![
    Rc::new(Product { name: "Laptop".to_string(), price: 999.99, category: "Electronics".to_string() }),
    Rc::new(Product { name: "Book".to_string(), price: 19.99, category: "Books".to_string() }),
];

// Apply discount only to electronics
let discounted_products = products
    .iter()
    .when_keypath(Product::category(), |cat| cat == "Electronics", |iter| {
        iter.map_keypath(Product::price(), |&price| price * 0.9)
    })
    .collect::<Vec<_>>();

Modules§

utils
Utility functions for common keypath operations

Structs§

KeyPathsChain
KeyPaths chain for composable operations

Traits§

ComposableIterator
Extension trait for adding composable operations to iterators

Functions§

chain_keypath_ops
Chain multiple keypath transformations
pipe
Function composition for keypath operations
unless_keypath
Inverse conditional operations
when_keypath
Conditional keypath operations