diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-02-13 15:21:00 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-02-13 15:21:00 -0500 |
| commit | c4479e4bd61fb2c68fdac2637a20d6d99f7b9552 (patch) | |
| tree | d6cc4335d64ad9d8be9e375f6e820cfcccd153df /stdlib | |
| parent | 5be955904682300153af0abdfd6b711b9da2ac8f (diff) | |
Add Int:onward() iterator
Diffstat (limited to 'stdlib')
| -rw-r--r-- | stdlib/integers.c | 15 | ||||
| -rw-r--r-- | stdlib/integers.h | 2 |
2 files changed, 17 insertions, 0 deletions
diff --git a/stdlib/integers.c b/stdlib/integers.c index 858025fa..835da97b 100644 --- a/stdlib/integers.c +++ b/stdlib/integers.c @@ -362,6 +362,14 @@ public PUREFUNC Closure_t Int$to(Int_t first, Int_t last, OptionalInt_t step) { return (Closure_t){.fn=_next_int, .userdata=range}; } +public PUREFUNC Closure_t Int$onward(Int_t first, Int_t step) { + IntRange_t *range = GC_MALLOC(sizeof(IntRange_t)); + range->current = first; + range->last = NONE_INT; + range->step = step; + return (Closure_t){.fn=_next_int, .userdata=range}; +} + public Int_t Int$from_str(const char *str) { mpz_t i; int result; @@ -575,6 +583,13 @@ public void Int32$deserialize(FILE *in, void *outval, Array_t*, const TypeInfo_t range->step = step.is_none ? (last >= first ? I_small(1) : I_small(-1)) : KindOfInt##_to_Int(step.i); \ return (Closure_t){.fn=_next_int, .userdata=range}; \ } \ + public to_attr Closure_t KindOfInt ## $onward(c_type first, c_type step) { \ + IntRange_t *range = GC_MALLOC(sizeof(IntRange_t)); \ + range->current = KindOfInt##_to_Int(first); \ + range->last = NONE_INT; \ + range->step = KindOfInt##_to_Int(step); \ + return (Closure_t){.fn=_next_int, .userdata=range}; \ + } \ public PUREFUNC Optional ## KindOfInt ## _t KindOfInt ## $parse(Text_t text) { \ OptionalInt_t full_int = Int$parse(text); \ if (full_int.small == 0) return (Optional ## KindOfInt ## _t){.is_none=true}; \ diff --git a/stdlib/integers.h b/stdlib/integers.h index fce243a9..7bed4551 100644 --- a/stdlib/integers.h +++ b/stdlib/integers.h @@ -35,6 +35,7 @@ Text_t type_name ## $octal(c_type i, Int_t digits, bool prefix); \ Array_t type_name ## $bits(c_type x); \ Closure_t type_name ## $to(c_type first, c_type last, Optional ## type_name ## _t step); \ + Closure_t type_name ## $onward(c_type first, c_type step); \ PUREFUNC Optional ## type_name ## _t type_name ## $parse(Text_t text); \ MACROLIKE PUREFUNC c_type type_name ## $clamped(c_type x, c_type min, c_type max) { \ return x < min ? min : (x > max ? max : x); \ @@ -102,6 +103,7 @@ Text_t Int$format(Int_t i, Int_t digits); Text_t Int$hex(Int_t i, Int_t digits, bool uppercase, bool prefix); Text_t Int$octal(Int_t i, Int_t digits, bool prefix); PUREFUNC Closure_t Int$to(Int_t first, Int_t last, OptionalInt_t step); +PUREFUNC Closure_t Int$onward(Int_t first, Int_t step); OptionalInt_t Int$from_str(const char *str); OptionalInt_t Int$parse(Text_t text); Int_t Int$abs(Int_t x); |
