partial_functional/
lib.rs

1//! # Partial Functional types for Rust
2//!
3//! I have tried taking some functional programming types into Rust types.
4//! You will find types that are in the group of [Semigroup] and [Monoid]s as well as [Functor](functor::Functor).
5//!
6//! It is meant to bring some more functional programming tricks in to rust.
7//!
8//! ## Examples
9//! ```
10//! use partial_functional::prelude::*;
11//!
12//! #[derive(Debug, PartialEq)]
13//! struct OrderLine {
14//!     product_code: String,
15//!     quantity: Sum<u32>,
16//!     price: Sum<f32>,
17//! }
18//!
19//! impl Semigroup for OrderLine {
20//!     fn combine(self, other: Self) -> Self {
21//!         Self {
22//!             product_code: String::from("TOTAL"),
23//!             quantity: self.quantity.combine(other.quantity),
24//!             price: self.price.combine(other.price),
25//!         }
26//!     }
27//! }
28//!
29//! impl Monoid for OrderLine {
30//!     fn empty() -> Self {
31//!         Self { product_code: String::from(""), quantity: Sum::empty(), price: Sum::empty() }
32//!     }
33//! }
34//!
35//! let order_lines = vec![
36//!     OrderLine { product_code: String::from("AAA"), quantity: Sum(2), price: Sum(19.98) },
37//!     OrderLine { product_code: String::from("BBB"), quantity: Sum(1), price: Sum(1.99) },
38//!     OrderLine { product_code: String::from("CCC"), quantity: Sum(3), price: Sum(3.99) },
39//! ];
40//!
41//! let mut total = order_lines
42//!     .into_iter()
43//!     .fold(OrderLine::empty(), |acc, item| acc.combine(item));
44//!
45//! let expected = OrderLine { product_code: "TOTAL".into(), quantity: 6.into(), price: 25.96.into() };
46//! assert_eq!(expected, total);
47//!
48//! let new_line = OrderLine { product_code: "DDD".into(), quantity: 1.into(), price: 29.98.into() };
49//! total = total.combine(new_line);
50//!
51//! let expected = OrderLine { product_code: "TOTAL".into(), quantity: 7.into(), price: 55.94.into() };
52//! assert_eq!(expected, total);
53//! ```
54//!
55//! A more elaborate example of the above can be run with `cargo run --example orderline`
56
57pub mod functor;
58pub mod hkt;
59pub mod monoid;
60pub mod semigroup;
61
62pub use hkt::*;
63pub use monoid::{All, Any, First, Last, Monoid, Product, Sum, Min, Max};
64pub use semigroup::Semigroup;
65
66pub mod prelude {
67    pub use crate::{
68        monoid::{All, Any, First, Last, Monoid, Product, Sum, Min, Max},
69        semigroup::Semigroup,
70    };
71}