Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotated Examples #22

Open
Veykril opened this issue Oct 1, 2020 · 1 comment
Open

Annotated Examples #22

Veykril opened this issue Oct 1, 2020 · 1 comment
Labels
discussion enhancement New feature or request

Comments

@Veykril
Copy link
Owner

Veykril commented Oct 1, 2020

The old book had an annotated examples section which basically was meant to show some more complex real world macros with comments explaining it more in-depth.

See https://danielkeep.github.io/tlborm/book/aeg-README.html

@Veykril Veykril added discussion enhancement New feature or request labels Oct 1, 2020
@guigo613
Copy link

guigo613 commented Mar 24, 2023

in section 2.2 there is a good example of a macro
playing a little I created a simpler macro and would like to share it in case it is useful

use std::iter::from_fn;

macro_rules! recur {
    ($x:ident[$y:ident]: $t:ty = $($a:literal),+, ..., $b:expr) => {
        {
            let mut $x = [$($a),+];
            let mut $y = 0;
            let len = $x.len();
            from_fn::<$t, _>(move || {
                if $y < len {
                    let b = Some($x[$y]);
                    $y += 1;
                    
                    b
                } else if len > 1 {
                    let b = $b;
                    $x.copy_within(1.., 0);
                    $x[len - 1] = b;
                    Some(b)
                } else {
                    $x[0] = $b;
                    Some($x[0])
                }
            })
        }
    }
}

let re = recur!(a[n]: usize = 0, 1, 1, 2, 3, 5, ..., a[n-2] + a[n-1]);
    
for r in re.skip(3).take(10) {
    println!("{r}");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants