rust2fun (pronounced: rʌstafʌn)
A library for functional programming in Rust.
Build
By default, the library is built with the std
feature enabled. To disable it, use the --no-default-features
flag.
Usage
Add this to your Cargo.toml
:
[]
= "0.2.1"
and import the prelude:
use *;
Supported features
Combinators:
- function composition (with compose macro)
- pipelines (with pipe macro)
- currying
- argument flipping (with flip macro)
- constant functions
- id (I combinator)
- apply (A combinator)
- apply_to (T combinator)
- substitution (S combinator)
- converge
- on (Psi combinator)
- if_else
- fix (Y combinator)
- no operation
- tuple constructors
Type classes:
- Semigroup
- Monoid
- Semigroupal
- Invariant
- Functor
- Bifunctor
- Pure
- AndThen
- Apply
- Applicative
- FlatMap
- Monad + ( bind! notation )
Data types:
- NEVec (non-empty vector)
- Validated
- ValidatedNev
Examples
- Function
print_user_credit_card
accepts user(s) wrapped in any effect (Option, Result, Vec, etc.) and prints corresponding credit card(s).
...usage:
print_credit_card_of;
print_credit_card_of;
- Validation accumulating all errors.
Assuming we have the following validation rules that need to be applied to create a new credit card:
...we can create a new credit card by applying all validation rules and collecting all errors in a vector Vec
,
non-empty vector NEVec
(like in the example) or other semigroup (e.g. String
, u32
, etc.):
...alternatively, this can be done using the map3
method:
bind!
notation for monads (likedo
notation in Haskell orfor
comprehension in Scala):
Assuming we have the following functions defined:
...we can use bind!
notation to calculate daily profit for each asset:
let profits: = bind! ;
Release notes
0.1.0 (2023-01-22)
- Initial release: combinators, Semigroupal, Invariant, Functor, Apply, Applicative, FlatMap, Monad
0.2.0 (2023-09-10)
- The project got its logo (thanks olasinitsyna)
- Moved macros imports to the prelude
- Added
noopX
andtupleX
sets of functions - Added type classes: Semigroup, Monoid, Bifunctor + Higher2 (thanks lrind)
- Added data types: NEVec, Validated
- Added
bind!
notation - Multiple fixes and improvements
0.2.1 (2023-09-21)
- Fixed Semigroupal and Apply behavior (thanks GoldsteinE for the report)
- Added type classes: Pure, AndThen
- Refactored
mapX
andapX
functions