From: Brendan Hansen Date: Sat, 7 Jan 2023 00:02:33 +0000 (-0600) Subject: renamed iter.as_iterator to iter.as_iter X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9e67790daf4cf00cf53bec386a1e820ec5e9c380;p=onyx.git renamed iter.as_iterator to iter.as_iter --- diff --git a/core/container/bucket_array.onyx b/core/container/bucket_array.onyx index 52918f54..0cfb8d5e 100644 --- a/core/container/bucket_array.onyx +++ b/core/container/bucket_array.onyx @@ -101,8 +101,8 @@ for_each :: macro (b: Bucket_Array($T), body: Code) { } } -#match iter.as_iterator iterator -iterator :: (b: ^Bucket_Array($T)) -> Iterator(T) { +#match iter.as_iter as_iter +as_iter :: (b: ^Bucket_Array($T)) -> Iterator(T) { Context :: struct (T: type_expr) { ba : ^Bucket_Array(T); bucket_idx : i32; diff --git a/core/container/iter.onyx b/core/container/iter.onyx index fba14a0a..ed58561d 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -2,10 +2,10 @@ package core.iter use core {memory, alloc, array, Pair} -as_iterator :: #match {} +as_iter :: #match {} Iterable :: interface (t: $T) { - { as_iterator(t) } -> Iterator; + { as_iter(t) } -> Iterator; } close :: (it: Iterator($T)) { @@ -31,11 +31,36 @@ ImplicitIterator :: interface (t: $T) { } #overload #precedence 10000 -as_iterator :: (x: ^$T/ImplicitIterator) => { +as_iter :: (x: ^$T/ImplicitIterator) => { x->iter_open(); return generator_no_copy(x, T.iter_next, T.iter_close); } +// nocheckin +// Does it make sense for the language to have +// the iterator "methods" as actual methods +// on every iterator type? Or should you +// just have to use the `iter.` variants +// instead? +#inject Iterator { + filter :: filter; + map :: map; + zip :: zip; + + take_one :: take_one; + take :: take; + take_while :: take_while; + skip :: skip; + skip_while :: skip_while; + + enumerate :: enumerate; + + fold :: fold; + count :: count; + some :: some; + every :: every; +} + // Iterator Modifiers @@ -503,9 +528,9 @@ enumerate :: #match #local {} #overload enumerate :: macro (it: $T/Iterable, start_index: i32 = 0) => { - as_iterator :: as_iterator + as_iter :: as_iter enumerate :: enumerate - return enumerate(as_iterator(it), start_index); + return enumerate(as_iter(it), start_index); } #overload @@ -583,10 +608,10 @@ from_array :: (arr: [] $T) => generator( #overload -as_iterator :: from_array +as_iter :: from_array #overload -as_iterator :: (x: ^[..] $T) -> Iterator(T) { +as_iter :: (x: ^[..] $T) -> Iterator(T) { Context :: struct (T: type_expr) { arr: ^[..] T; current: u32; @@ -622,7 +647,7 @@ as_iterator :: (x: ^[..] $T) -> Iterator(T) { } #overload -as_iterator :: (x: ^[..] $T, by_pointer: bool) -> Iterator(^T) { +as_iter :: (x: ^[..] $T, by_pointer: bool) -> Iterator(^T) { Context :: struct (T: type_expr) { arr: ^[..] T; current: u32; @@ -658,7 +683,7 @@ as_iterator :: (x: ^[..] $T, by_pointer: bool) -> Iterator(^T) { } #overload -as_iterator :: (r: range) -> Iterator(i32) { +as_iter :: (r: range) -> Iterator(i32) { Context :: struct { r: range; v: i32; @@ -699,7 +724,7 @@ fold :: #match #local {} #overload fold :: macro (it: $T/Iterable, init: $R, combine: $S) => { fold :: fold - return fold(as_iterator(it), init, combine); + return fold(as_iter(it), init, combine); } #overload @@ -716,7 +741,7 @@ count :: #match #local {} #overload count :: macro (it: $T/Iterable, cond: $F) => { count :: count - return count(as_iterator(it), cond); + return count(as_iter(it), cond); } #overload @@ -731,8 +756,8 @@ some :: #match #local {} #overload some :: macro (it: $T/Iterable, cond: $F) => { some :: some - as_iterator :: as_iterator - return some(as_iterator(it), cond); + as_iter :: as_iter + return some(as_iter(it), cond); } #overload @@ -746,8 +771,8 @@ every :: #match #local {} #overload every :: macro (it: $T/Iterable, cond: $F) => { every :: every - as_iterator :: as_iterator - return every(as_iterator(it), cond); + as_iter :: as_iter + return every(as_iter(it), cond); } #overload @@ -757,13 +782,13 @@ every :: (it: Iterator($T), cond: (T) -> bool) -> bool { } prod :: (x: $I1/Iterable, y: $I2/Iterable) => { - y_iter := as_iterator(y); + y_iter := as_iter(y); y_val, _ := take_one(y_iter); return generator( ^.{ x = x, - x_iter = as_iterator(x), + x_iter = as_iter(x), y_iter = y_iter, y_val = y_val @@ -778,7 +803,7 @@ prod :: (x: $I1/Iterable, y: $I2/Iterable) => { ctx.y_val, cont = take_one(ctx.y_iter); if !cont do return .{}, false; - ctx.x_iter = as_iterator(ctx.x); + ctx.x_iter = as_iter(ctx.x); x_val, cont = take_one(ctx.x_iter); if !cont do return .{}, false; @@ -828,9 +853,9 @@ comp :: macro (i: Iterator($V), value: Code) => { #overload comp :: macro (i: $I/Iterable, value: Code) => { - as_iterator :: as_iterator + as_iter :: as_iter comp :: comp - return comp(as_iterator(i), value); + return comp(as_iter(i), value); } @@ -882,8 +907,8 @@ generator_no_copy :: (ctx: ^$Ctx, gen: (^Ctx) -> ($T, bool), close: (^Ctx) -> vo #overload distributor :: macro (it: $T/Iterable) => { distributor :: distributor; - as_iterator :: as_iterator; - return distributor(as_iterator(it)); + as_iter :: as_iter; + return distributor(as_iter(it)); } #overload @@ -928,16 +953,16 @@ generator_no_copy :: (ctx: ^$Ctx, gen: (^Ctx) -> ($T, bool), close: (^Ctx) -> vo #overload parallel_for :: macro (iterable: $I/Iterable, thread_count: u32, thread_data: ^$Ctx, body: Code) { parallel_for :: parallel_for; - as_iterator :: as_iterator; + as_iter :: as_iter; - parallel_for(as_iterator(iterable), thread_count, thread_data, body); + parallel_for(as_iter(iterable), thread_count, thread_data, body); } #overload parallel_for :: macro (iter: Iterator($T), thread_count: u32, thread_data: ^$Ctx, body: Code) { use core {thread, alloc} distributor :: distributor; - as_iterator :: as_iterator; + as_iter :: as_iter; if thread_count != 0 { dist := distributor(iter); diff --git a/core/container/list.onyx b/core/container/list.onyx index 0838fda9..aedfd41b 100644 --- a/core/container/list.onyx +++ b/core/container/list.onyx @@ -149,7 +149,7 @@ map :: #match #local {} } -#match core.iter.as_iterator as_iter +#match core.iter.as_iter as_iter as_iter :: (list: ^List) => core.iter.generator(^.{current = list.first}, (ctx) => { if ctx.current != null { diff --git a/core/container/map.onyx b/core/container/map.onyx index c5986139..b17adb0a 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -183,8 +183,7 @@ literal :: ($Key: type_expr, $Value: type_expr, values: [] MapLiteralValue(Key, } -#overload core.iter.as_iterator as_iter - +#overload core.iter.as_iter as_iter as_iter :: (m: ^Map) => core.iter.generator( ^.{ m = m, i = 0 }, diff --git a/core/container/set.onyx b/core/container/set.onyx index 066733fe..1ac375b3 100644 --- a/core/container/set.onyx +++ b/core/container/set.onyx @@ -118,8 +118,7 @@ empty :: (use set: ^Set) -> bool { return entries.count == 0; } -#overload core.iter.as_iterator as_iter - +#overload core.iter.as_iter as_iter as_iter :: (s: ^Set) => core.iter.generator( ^.{ s = s, i = 0 }, diff --git a/core/misc/arg_parse.onyx b/core/misc/arg_parse.onyx index 43dc396f..623e857c 100644 --- a/core/misc/arg_parse.onyx +++ b/core/misc/arg_parse.onyx @@ -3,7 +3,7 @@ package core.arg_parse use core arg_parse :: (c_args: [] cstr, output: any) -> bool { - arg_iter := iter.as_iterator(c_args) + arg_iter := iter.as_iter(c_args) |> iter.map(string.from_cstr); defer arg_iter.close(arg_iter.data); @@ -55,4 +55,4 @@ arg_parse :: (c_args: [] cstr, output: any) -> bool { } return true; -} \ No newline at end of file +} diff --git a/core/net/tcp.onyx b/core/net/tcp.onyx index 18864fb5..696501b9 100644 --- a/core/net/tcp.onyx +++ b/core/net/tcp.onyx @@ -321,7 +321,7 @@ tcp_server_broadcast :: (use server: ^TCP_Server, data: [] u8, except: ^TCP_Serv tcp_server_handle_events :: macro (server: ^TCP_Server, handler: Code) { while server->pulse() { - for iter.as_iterator(^server.connection) { + for iter.as_iter(^server.connection) { switch it.kind do #unquote handler; } } diff --git a/examples/20_auto_return.onyx b/examples/20_auto_return.onyx index 5a52db09..04139700 100644 --- a/examples/20_auto_return.onyx +++ b/examples/20_auto_return.onyx @@ -34,7 +34,7 @@ main :: (args: [] cstr) { return arr; } - return consume_inner(iter.as_iterator(it)); + return consume_inner(iter.as_iter(it)); } diff --git a/examples/21_quick_functions.onyx b/examples/21_quick_functions.onyx index 2a453577..d43abc84 100644 --- a/examples/21_quick_functions.onyx +++ b/examples/21_quick_functions.onyx @@ -35,7 +35,7 @@ main :: (args) => { println(quickest_no_types(3, 4.0f)); // This is an example of what the iter package looks like. - val := iter.as_iterator(range.{ 20, 1, -1 }) + val := iter.as_iter(range.{ 20, 1, -1 }) |> iter.map(x => x * 2) |> iter.map(x => cast(f32) x) |> iter.filter(x => x > 20) diff --git a/tests/aoc-2021/day03.onyx b/tests/aoc-2021/day03.onyx index db94746e..243ea239 100644 --- a/tests/aoc-2021/day03.onyx +++ b/tests/aoc-2021/day03.onyx @@ -72,11 +72,11 @@ main :: (args) => { } } - for iter.as_iterator(range.{ BITS - 1, 0, -1 }) { + for iter.as_iter(range.{ BITS - 1, 0, -1 }) { filter_array(oxygen_array, it, #(A >= B)); filter_array(co2_array, it, #(A < B)); } printf("Part 2: {}\n", oxygen_array[0] * co2_array[0]); } -} \ No newline at end of file +} diff --git a/tests/aoc-2021/day07.onyx b/tests/aoc-2021/day07.onyx index f4fb8c08..e96d3c57 100644 --- a/tests/aoc-2021/day07.onyx +++ b/tests/aoc-2021/day07.onyx @@ -7,7 +7,7 @@ main :: (args) => { reader := io.reader_make(file); nums := io.read_all(^reader) |> string.split(#char ",") - |> iter.as_iterator() + |> iter.as_iter() |> iter.map((x) => cast(i32) conv.parse_int(x)) |> iter.to_array(); @@ -31,4 +31,4 @@ main :: (args) => { printf("Part 2: {} with fuel {}\n", best_middle, min_cost); } -} \ No newline at end of file +} diff --git a/tests/aoc-2021/day09.onyx b/tests/aoc-2021/day09.onyx index 2142a042..9adce766 100644 --- a/tests/aoc-2021/day09.onyx +++ b/tests/aoc-2021/day09.onyx @@ -120,11 +120,11 @@ main :: (args) => { array.quicksort(lowest_count.entries, (a, b) => b.value - a.value); - answer := iter.as_iterator(lowest_count.entries) + answer := iter.as_iter(lowest_count.entries) |> iter.map((x) => x.value) |> iter.take(3) |> iter.fold(1, (x, y) => x * y); printf("Part 2: {}\n", answer); } -} \ No newline at end of file +} diff --git a/tests/aoc-2021/day11.onyx b/tests/aoc-2021/day11.onyx index 74c41712..c4346348 100644 --- a/tests/aoc-2021/day11.onyx +++ b/tests/aoc-2021/day11.onyx @@ -50,7 +50,7 @@ main :: (args) => { } } - for flash: iter.as_iterator(^to_flash) { + for flash: iter.as_iter(^to_flash) { for y: -1 .. 2 do for x: -1 .. 2 { if y == 0 && x == 0 do continue; @@ -60,7 +60,7 @@ main :: (args) => { } } - for flash: iter.as_iterator(^to_flash) { + for flash: iter.as_iter(^to_flash) { set_octopus(flash.x, flash.y, 0); if step <= 100 do flash_count += 1; } diff --git a/tests/aoc-2021/day12.onyx b/tests/aoc-2021/day12.onyx index 51d30757..3d701d04 100644 --- a/tests/aoc-2021/day12.onyx +++ b/tests/aoc-2021/day12.onyx @@ -45,11 +45,11 @@ main :: (args) => { *name_copy = name; return iter.concat( - iter.as_iterator(edges) + iter.as_iter(edges) |> iter.filter(name_copy, (x, n) => x.a == *n) |> iter.map(x => x.b), - iter.as_iterator(edges) + iter.as_iter(edges) |> iter.filter(name_copy, (x, n) => x.b == *n) |> iter.map(x => x.a) ); @@ -61,7 +61,7 @@ main :: (args) => { } edge_map: Map(str, [] str); - for v: iter.as_iterator(^verticies) { + for v: iter.as_iter(^verticies) { edge_map[*v] = children_of(^edges, *v) |> iter.to_array(); } diff --git a/tests/auto_poly.onyx b/tests/auto_poly.onyx index 84de9c4e..6c1a15cb 100644 --- a/tests/auto_poly.onyx +++ b/tests/auto_poly.onyx @@ -31,7 +31,7 @@ f :: (thing: ^Thing, other: typeof thing) { main :: (args) => { m: Map(i32, str); - dumb(iter.as_iterator(1 .. 10), ^m); + dumb(iter.as_iter(1 .. 10), ^m); V :: Vector2(i32) v1 := V.{ 1, 0 }; @@ -43,7 +43,7 @@ main :: (args) => { t: Thing(f32); f(^t, ^t); - for iter.as_iterator(0 .. 100) |> iter.map(x => x * 2) |> iter.filter(x => x <= 10) { + for iter.as_iter(0 .. 100) |> iter.map(x => x * 2) |> iter.filter(x => x <= 10) { println(it); } } diff --git a/tests/bucket_array.onyx b/tests/bucket_array.onyx index c449fcbf..550dbfa3 100644 --- a/tests/bucket_array.onyx +++ b/tests/bucket_array.onyx @@ -14,9 +14,9 @@ main :: (args: [] cstr) { sum += *it; }); - for it: bucket_array.iterator(^ba) { + for it: bucket_array.as_iter(^ba) { printf("{}\n", it); } printf("Sum is {}\n", sum); -} \ No newline at end of file +} diff --git a/tests/interfaces.onyx b/tests/interfaces.onyx index 6e51366e..8ba891ef 100644 --- a/tests/interfaces.onyx +++ b/tests/interfaces.onyx @@ -89,7 +89,7 @@ consume :: macro (x: $T/iter.Iterable) => { }, } - iterator := iter.as_iterator(x); + iterator := iter.as_iter(x); return consume_inner(iterator); } diff --git a/tests/lazy_iterators.onyx b/tests/lazy_iterators.onyx index ae30bd3e..42b630f9 100644 --- a/tests/lazy_iterators.onyx +++ b/tests/lazy_iterators.onyx @@ -40,7 +40,7 @@ main :: (args: [] cstr) { } } - iterator := iter.as_iterator(1 .. 11) + iterator := iter.as_iter(1 .. 11) |> iter.map((x: i32) -> i32 { return x * 2; }) |> iter.filter((x: i32) -> bool { return x > 10; }) |> iter.map((x: i32) -> i32 { return x + 42; }); diff --git a/tests/remove_test.onyx b/tests/remove_test.onyx index 7b1077fc..ddddedb9 100644 --- a/tests/remove_test.onyx +++ b/tests/remove_test.onyx @@ -8,11 +8,11 @@ main :: (args) => { println(x); - for iter.as_iterator(^x) { + for iter.as_iter(^x) { if it % 2 == 0 { #remove; } } println(x); -} \ No newline at end of file +}