Introduction
Rustica brings powerful abstractions from category theory and functional programming to the Rust ecosystem, providing a rich set of type classes, data types, and utilities commonly found in functional programming languages.
A comprehensive functional programming library for Rust
Rustica brings powerful abstractions from category theory and functional programming to the Rust ecosystem, providing a rich set of type classes, data types, and utilities commonly found in functional programming languages.
Add Rustica to your Cargo.toml
:
[dependencies]
rustica = "0.7.1"
[dependencies]
rustica = { version = "0.7.1", features = ["async"] }
[dependencies]
rustica = { version = "0.7.1", features = ["pvec"] }
Then import the prelude in your Rust code:
use rustica::prelude::*;
Functor
, Applicative
, Monad
Semigroup
, Monoid
Traversable
, Foldable
Bifunctor
, Comonad
Maybe<T>
, Either<L, R>
IO<A>
, State<S, A>
Reader<E, A>
, Writer<W, A>
Cont<R, A>
, AsyncMonad<A>
StateT<S, M, A>
ReaderT<E, M, A>
WriterT<W, M, A>
Lens
and Prism
IsoLens
and IsoPrism
use rustica::prelude::*;
use rustica::datatypes::id::Id;
use rustica::traits::identity::Identity;
// Create Id values
let x = Id::new(5);
let y = Id::new(3);
let z = Id::new(2);
// Access the inner value
assert_eq!(*x.value(), 5);
// Map over the value
let doubled = x.fmap(|n| n * 2);
assert_eq!(*doubled.value(), 10);
// Chain computations
let result = x.and_then(|a|
y.and_then(move |b|
z.fmap(move |c| a + b + c)
)
);
assert_eq!(*result.value(), 10); // 5 + 3 + 2