From 9081418f0d93eade4599a411697948a069e43915 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 7 Dec 2023 22:32:29 -0600 Subject: [PATCH] added: `iter.counter` --- core/container/iter.onyx | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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 // -- 2.25.1