From: Brendan Hansen Date: Fri, 8 Dec 2023 04:32:29 +0000 (-0600) Subject: added: `iter.counter` X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9081418f0d93eade4599a411697948a069e43915;p=onyx.git added: `iter.counter` --- diff --git a/core/container/iter.onyx b/core/container/iter.onyx index e37de430..204c9089 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -93,6 +93,25 @@ empty :: ($T: type_expr) -> Iterator(T) { }; } + +#doc """ + Helper function to create an infinite counting iterator. + + Use `start` to configure the starting value. + + Use `type` to configure the type used for the iterator. +""" +counter :: (start: type = 0, $type: type_expr = i32) -> Iterator(type) { + return generator( + &.{ i = start }, + ctx => { + defer ctx.i += 1; + return ctx.i, true; + } + ); +} + + // // Implicit iterator creation //