Skip to content

Commit

Permalink
added: overloads to Slice.map and Slice.fold1
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanfh committed Aug 1, 2024
1 parent f7fea9a commit 48b180a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
13 changes: 11 additions & 2 deletions core/container/iter.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ use core.intrinsics.types {type_is_struct}
//
// Iterator is a builtin type known by the compiler, as Iterators
// can be used in for-loops natively without any translation.

Iterator.from :: as_iter
Iterator.next :: next
Iterator.next_opt :: next_opt
Iterator.empty :: empty
Iterator.counter :: counter

Iterator.filter :: filter;
Iterator.map :: map;
Iterator.flat_map :: flat_map;
Expand All @@ -37,8 +44,10 @@ Iterator.sum :: sum;
Iterator.collect :: to_array;
Iterator.collect_map :: to_map;

Iterator.from :: as_iter
Iterator.next :: next
Iterator.generator :: generator
Iterator.generator_no_copy :: generator_no_copy
Iterator.comp :: comp
Iterator.prod :: prod



Expand Down
53 changes: 50 additions & 3 deletions core/container/slice.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,41 @@ Slice.copy :: (sl: [] $T, allocator := context.allocator) -> [] T {


/// Creates a new slice and populates by performing the transform function on each element of the existing slice.
Slice.map :: #match #local {}

#overload
Slice.map :: (sl: [] $T, transform: (T) -> $R, allocator := context.allocator) -> [] R {
new_slice := Slice.make(R, sl.count, allocator)
for v, i in sl {
new_slice[i] = transform(v)
}
for v, i in sl do new_slice[i] = transform(v)
return new_slice
}

#overload
Slice.map :: macro (sl: [] $T, transform: Code, allocator := context.allocator) => {
_s := sl
new_slice := Slice.make(typeof #unquote transform(sl[0]), _s.count, allocator)
for v, i in _s do new_slice[i] = #unquote transform(v)
return new_slice
}


/// Modifies a slice in-place without allocating any memory and returns the original slice.
Slice.map_inplace :: #match #local {}

#overload
Slice.map_inplace :: (sl: [] $T, transform: (T) -> T, allocator := context.allocator) => {
for &v in sl do *v = transform(*v)
return sl
}

#overload
Slice.map_inplace :: macro (sl: [] $T, transform: Code, allocator := context.allocator) => {
_s := sl
for &v in _s do *v = #unquote transform(*v)
return _s
}


/// Moves an element to a new index, ensuring that order of other elements is retained.
///
/// use core {slice, println}
Expand Down Expand Up @@ -369,6 +395,27 @@ Slice.fold :: macro (arr: [] $T, init: $R, body: Code) -> R {
return acc;
}


Slice.fold1 :: #match #local {}

#overload
Slice.fold1 :: (arr: [] $T, f: (T, T) -> T) -> ? T {
if arr.count == 0 do return .None

val := arr[0];
for it in arr[1 .. arr.count] do val = f(it, val);
return val;
}

#overload
Slice.fold1 :: macro (arr: [] $T, body: Code) -> ? T {
if arr.count == 0 do return .None

acc := arr[0];
for it in arr[1 .. arr.count] do acc = #unquote body(it, acc);
return acc;
}

/// Returns `true` if *every* element in the slice meets the predicate test.
Slice.every :: #match #local {}

Expand Down

0 comments on commit 48b180a

Please sign in to comment.