funlib_macros/lib.rs
1//!
2//! Macros for use with the functional library
3//!
4
5/// Derive HKT macro to create Higer Kinded Types
6#[macro_export]
7macro_rules! hkt {
8 ($t:ident) => {
9 impl<B, C> HKT<C> for $t<B> {
10 type A = B;
11 type M = $t<C>;
12 }
13 impl<'a, B: 'a> HKST<'a, B> for $t<B> {
14 type A = &'a B;
15 type M = $t<&'a B>;
16 }
17 };
18}
19
20/// Compose functions
21///
22/// # Examples
23///
24// / ```
25// / # #[macro_use] extern crate funlib;
26// / # fn main() {
27// / fn add1(i: i32) -> i32 { i + 1 }
28// / fn double(i: i32) -> i32 { i * 2 }
29// /
30// / let c = compose!(add1, double);
31// / assert_eq!(4, c(1));
32// / # }
33// / ```
34#[macro_export]
35macro_rules! compose {
36 ( $last:expr ) => { $last };
37 ( $head:expr, $($tail:expr), +) => {
38 funlib_macros::compose_two($head, compose!($($tail),+))
39 };
40}
41/// Used in compose macro to compose functions together
42pub fn compose_two<A, B, C, G, F>(f: F, g: G) -> impl Fn(A) -> C
43where F: Fn(A) -> B, G: Fn(B) -> C {
44 move |x| g(f(x))
45}