Crate partial_functional

Source
Expand description

§Partial Functional types for Rust

I have tried taking some functional programming types into Rust types. You will find types that are in the group of Semigroup and Monoids as well as Functor.

It is meant to bring some more functional programming tricks in to rust.

§Examples

use partial_functional::prelude::*;

#[derive(Debug, PartialEq)]
struct OrderLine {
    product_code: String,
    quantity: Sum<u32>,
    price: Sum<f32>,
}

impl Semigroup for OrderLine {
    fn combine(self, other: Self) -> Self {
        Self {
            product_code: String::from("TOTAL"),
            quantity: self.quantity.combine(other.quantity),
            price: self.price.combine(other.price),
        }
    }
}

impl Monoid for OrderLine {
    fn empty() -> Self {
        Self { product_code: String::from(""), quantity: Sum::empty(), price: Sum::empty() }
    }
}

let order_lines = vec![
    OrderLine { product_code: String::from("AAA"), quantity: Sum(2), price: Sum(19.98) },
    OrderLine { product_code: String::from("BBB"), quantity: Sum(1), price: Sum(1.99) },
    OrderLine { product_code: String::from("CCC"), quantity: Sum(3), price: Sum(3.99) },
];

let mut total = order_lines
    .into_iter()
    .fold(OrderLine::empty(), |acc, item| acc.combine(item));

let expected = OrderLine { product_code: "TOTAL".into(), quantity: 6.into(), price: 25.96.into() };
assert_eq!(expected, total);

let new_line = OrderLine { product_code: "DDD".into(), quantity: 1.into(), price: 29.98.into() };
total = total.combine(new_line);

let expected = OrderLine { product_code: "TOTAL".into(), quantity: 7.into(), price: 55.94.into() };
assert_eq!(expected, total);

A more elaborate example of the above can be run with cargo run --example orderline

Re-exports§

pub use monoid::All;
pub use monoid::Any;
pub use monoid::First;
pub use monoid::Last;
pub use monoid::Monoid;
pub use monoid::Product;
pub use monoid::Sum;
pub use monoid::Min;
pub use monoid::Max;
pub use semigroup::Semigroup;
pub use hkt::*;

Modules§

functor
hkt
monoid
prelude
semigroup

Macros§

combine
This is just a small convienience macro to chain several combines together. Everthing after the first expression has to have a From implementation for that type.