Skip to content

Conversation

NotsoanoNimus
Copy link
Contributor

This pull request depends on changes from #2378 -- that PR should be merged before this one. I will fix merge conflicts.


Adds a compile-time and runtime range functionality, similar to python's range function.

    // compile-time ranging
    $foreach $sample : @range(17):
        $echo @sprintf("SAMPLE: %s", $sample);   // prints "SAMPLE: 0" -> "SAMPLE: 16"
    $endforeach

    // runtime ranging - prints "[0, 2, 4, 6]"
    io::printfn("%s", array::trange(stop: 6, step: 2, inclusive: true));
    // below prints "[-2, 0, 2, 4, 6]"
    io::printfn("%s", array::trange(-2, 6, 2, true));

Compile-time ranges are fun to splat with:

    isz[] some_sequence = { 1, 2, 3, ...@range(4, 150, $inclusive: true) });

@lerno
Copy link
Collaborator

lerno commented Aug 11, 2025

This seems to overlap a bit with math::iota ?

@NotsoanoNimus
Copy link
Contributor Author

This seems to overlap a bit with math::iota ?

There is overlap for sure, but only where the desired array is sequential, from zero, and bound to a type's width; like array::trange(64) or @range(64) ==> math::iota(char[64]). Except iota produces char[64] and the others produce isz[].

However, the sequence of numbers produced by range can be more expressive and dynamic, so iota is really only a subset of it. If I wanted to generate test inputs or array indexes like [-10, -7, -4, -1, 2, ...] (imagine this is much larger), for example, iota cannot do that.

@NotsoanoNimus
Copy link
Contributor Author

Fixed merge conflicts. Added the ability to specify the resulting array's type, so it can be more flexible as needed. Also introduced a few more related unit tests.

This will now be marked ready for review, provided all CI checks pass.

@NotsoanoNimus NotsoanoNimus marked this pull request as ready for review August 13, 2025 22:06
@lerno
Copy link
Collaborator

lerno commented Aug 16, 2025

You could extend this to be usable with any type supporting + / and comparison.

Using $defined(start_stop + 1) and $defined(start_stop + step) and so on. You can start by writing the general code, then add the constraints.

@lerno
Copy link
Collaborator

lerno commented Sep 24, 2025

Ping!

@NotsoanoNimus
Copy link
Contributor Author

@lerno I still plan on making your suggested changes! This fell by the wayside thanks to an extended vacation, but I'm working on catching up mentally to any language changes since my absence. Please give this another week or so for an update with your changes to appear (hopefully less), thanks. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants