rec_map

Macro rec_map 

Source
macro_rules! rec_map {
    ($in_list:expr, $($func:tt)*) => { ... };
}
Expand description

Helper for writing const fn equivalents of NList::map

This macro acts like a function with this signature:

use nlist::{NList, PeanoInt};

use nlist::receiver::Receiver;

fn rec_map<'a, P, T, L, F, U>(list: P , func: F) -> NList<U, L>
where
    P: Receiver<'a, NList<T, L>>,
    L: PeanoInt,
    T: 'a,
    F: FnOnce(
        ... // parameter types explained below
    ) -> (U, NList<U, L::SubOneSat>)

The closure is only called when the list is non-empty (i.e.: when L != 0).

The closure parameters depend on the value of P:

  • If P == NList<T, L>: the parameters are (T, Nlist<T, L::SubOneSat>)
  • If P == &NList<T, L>: the parameters are (&T, &Nlist<T, L::SubOneSat>)
  • If P == &mut NList<T, L>: the parameters are (&mut T, &mut Nlist<T, L::SubOneSat>)

§Example

§By value

Example that takes an NList by value

use nlist::{NList, Peano, PeanoInt, nlist};
 
const LIST: NList<u128, Peano!(3)> = double(nlist![3, 5, 8]);
 
assert_eq!(LIST, nlist![6, 10, 16]);
 
const fn double<L>(list: NList<u128, L>) -> NList<u128, L>
where
    L: PeanoInt
{
    nlist::rec_map!{list, |elem: u128, next| (elem * 2, double(next))}
}

§By reference

Example that takes an NList by reference

use nlist::{NList, Peano, PeanoInt, nlist};
 
const LIST: NList<u128, Peano!(3)> = double(&nlist![3, 5, 8]);
 
assert_eq!(LIST, nlist![6, 10, 16]);
 
const fn double<L>(list: &NList<u128, L>) -> NList<u128, L>
where
    L: PeanoInt
{
    nlist::rec_map!{list, |elem: &u128, next| (*elem * 2, double(next))}
}