iter_utils
Source: functions.
Functions which operate on iterable types
Methods
Properties
incl
Alias for iter_utils.includes
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to check |
value |
T |
|
The value to check for |
start_index |
number |
Yes |
The index to start checking from Defaults to |
- Returns
-
boolean
includes
Checks if an iterable includes a value. Uses iter_utils.count.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to check |
value |
T |
|
The value to check for |
start_index |
number |
Yes |
The index to start checking from Defaults to |
- Returns
-
boolean
Methods
alleq(iter) → boolean
Checks if all items in an iterable are equal
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to check |
- Returns
-
boolean
append(array, ...items) → Array of T
Same as the built in Array.push
Parameters
Name | Type | Optional | Description |
---|---|---|---|
array |
Array of T |
|
The array to append to |
items |
T |
|
The items to append Value can be repeated. |
- Returns
-
Array of T
The resulting array
at(iter, index) → T
Gets the element at index
in iter
. Indices wrap around using
math_utils.wrapindex. If index
is a Vec, iter
will be
treated as a grid, or if index
is a list of integers, a list of elements
at those indices will be returned.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing (T or Iterable containing T) |
|
The iterable to get elements from |
index |
(number, Vec, or Array of number) |
|
The index/indicies to get |
- Returns
-
T
chunks(iter[, size]) → Array of Array of T
Creates a list of contiguous non-overlapping chunks of iter
, each of
length size
Examples
[1, 2, 3, 4, 5, 6].chunks() // [[1, 2], [3, 4], [5, 6]]
[1, 2, 3, 4, 5, 6].chunks(3) // [[1, 2, 3], [4, 5, 6]]
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to create chunks from |
size |
number |
Yes |
The size of each chunk Defaults to |
- Returns
-
Array of Array of T
count(iter, value[, string_like_match]) → number
Counts the number of times value
appears in iter
. If value
is a
function, it's will do the same as .filter(f).length
, if iter
is a
string or string_like_match
is explicitly set to true
, ["a", "b"]
will
match abcd
or ["a", "b", "c", "d"] as well as something like
["arstneio", ["a", "b], 4].
Examples
[4, 2, 3, 1, 2].count(2) // 2
"abababa".count("aba") // 2
[3, 2, 1, 2, 1].count([2, 1]) // 0
[3, 2, 1, 2, 1].count([2, 1], true) // 2
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to count values in |
value |
any |
|
The value to count |
string_like_match |
boolean |
Yes |
(See description) Defaults to |
- Returns
-
number
countol(iter, value[, string_like_match]) → number
Same as iter_utils.count
but counts overlapping matches
Examples
"abababa".countol("aba") // 3
[1, 2, 1, 2, 1].countol([1, 2, 1]) // 2
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to count values in |
value |
any |
|
The value to count |
string_like_match |
boolean |
Yes |
(See description) Defaults to |
- Returns
-
number
deltas(iter) → Array of number
Gets the differences between consecutive elements of an iterable
Example
[1, 2, 9, -4].deltas // [1, 7, -13]
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing number |
|
The iterable to get deltas from |
- Returns
-
Array of number
dropnth(iter, index) → Array of T
Removes the nth element from an iterable value
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to remove an element from |
index |
number |
|
The index of the element to remove |
- Returns
-
Array of T
The resulting array
each(iter, callback) → Iterable containing T
Same as the built in Array.forEach
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to iterate over |
callback |
function(T, number, Iterable containing T) |
|
The callback to call for each element (see the MDN link for more info) |
- Returns
-
Iterable containing T
The original iterable
everynth(iter, n[, start]) → Array of T
Return a list with every n
th element of iter
starting at index start
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to get elements from |
n |
number |
|
The step size |
start |
number |
Yes |
The starting index Defaults to |
- Returns
-
Array of T
filterout(array, filter_fn) → Array of T
Same as the built in Array.filter
but inverted. For consistency with builtins like Array.filter
and Array.map
which don't work on any iterable. This must also be called on an array.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
array |
Array of T |
|
The iterable to filter |
filter_fn |
function(T) |
|
The filter function |
- Returns
-
Array of T
first(iter) → any
Gets the first element of an iterable value
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to get the first element from |
- Returns
-
any
firstn(iter, n) → Array of T
Gets the first n
elements of an iterable value and returns them in an array
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to get elements from |
n |
number |
|
The number of elements to get |
- Returns
-
Array of T
flat(iter) → Array of T
Deep flattens an iterable
Example
[[1, [2]], [[[[3, [4, 5]]]]], 6].flat // [1, 2, 3, 4, 5, 6]
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to flatten |
- Returns
-
Array of T
for(iter, callback) → Iterable containing T
Same as the built in Array.forEach
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to iterate over |
callback |
function(T, number, Iterable containing T) |
|
The callback to call for each element (see the MDN link for more info) |
- Returns
-
Iterable containing T
The original iterable
iwhere(iter, fn) → Array of number
Returns list of indices where the function fn
returns true. fn
receives
the value, index and the iterable as arguments, in that order.
Example
[1, 2, 3, 4, 5].iwhere(x => x % 2 === 0) // [1, 3]
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
|
fn |
function(T, number, Iterable containing T) |
|
- Returns
-
Array of number
last(iter) → any
Gets the last element of an iterable value
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to get the last element from |
- Returns
-
any
lastn(iter, n) → Array of T
Gets the last n
elements of an iterable value and returns them in an array
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to get elements from |
n |
number |
|
The number of elements to get |
- Returns
-
Array of T
max(iter) → number
Same as the built in Math.max but it takes an iterable as a single argument
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to find the maximum of |
- Returns
-
number
min(iter) → number
Same as the built in Math.min but it takes an iterable as a single argument
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to find the minimum of |
- Returns
-
number
nflat(iter[, depth]) → Array of T
Flattens an iterable to a certain depth. Alias for the builtin Array.flat.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to flatten |
depth |
number |
Yes |
The depth to flatten to Defaults to |
- Returns
-
Array of T
none(array, fn) → boolean
Checks if there are no elements in array
that satisfy fn
. fn
is passed
to the built in Array.some
so refer to those docs if needed.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
array |
Array of T |
|
The array to check |
fn |
function(T, number, Array of T) |
|
The function to check |
- Returns
-
boolean
nsort(iter) → Array of T
Sorts an iterable (of numbers) in ascending order
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to sort |
- Returns
-
Array of T
The sorted array
perms(iter) → Array of Array of T
Generates all permutations of an iterable
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
- Returns
-
Array of Array of T
pieces(iter, n) → Array of Array of T
Splits an iterable into n
pieces. Rounds up if the split is uneven.
Examples
[1, 2, 3, 4, 5, 6].pieces(3) // [[1, 2], [3, 4], [5, 6]]
[1, 2, 3].pieces(2) // [[1, 2], [3]]
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to split |
n |
number |
|
The number of pieces to split into |
- Returns
-
Array of Array of T
prepend(array, ...items) → Array of T
Same as the built in Array.unshift
Parameters
Name | Type | Optional | Description |
---|---|---|---|
array |
Array of T |
|
The array to prepend to |
items |
T |
|
The items to prepend Value can be repeated. |
- Returns
-
Array of T
The resulting array
prod(iter) → number
Calculates the product of all values in an iterable
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable |
- Returns
-
number
rev(iter) → Array of T
Reverses the elements of an iterable (not in place)
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to reverse |
- Returns
-
Array of T
The reversed array
rsort(iter) → Array of T
Sorts an iterable (of numbers) in descending order
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to sort |
- Returns
-
Array of T
The sorted array
rsortby(iter, func) → Array of T
Sorts an iterable in descending order by the result of applying a given function to each element
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to sort |
func |
function(T) |
|
The function to apply to each element |
- Returns
-
Array of T
The sorted array
seqs(iter) → Array of Array of T
Returns a list of sublists containing consecutive elements of iter
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to group |
- Returns
-
Array of Array of T
sliding(iter, size[, wrap]) → Array of Array of T
Creates a list of sublists of length size
each of which is a sliding
window looking into iter
starting at index 0
moving along by 1 each
time. If loop
is true, there will be some extra windows at the end which
wrap around to the start.
Examples
[1, 2, 3, 4, 5].sliding(3) // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
[1, 2, 3].sliding(2, true) // [[1, 2], [2, 3], [3, 1]]
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing Array of T |
|
The iterable to create sliding windows from |
size |
number |
|
The size of the sliding window |
wrap |
boolean |
Yes |
Whether to include windows that wrap around Defaults to |
- Returns
-
Array of Array of T
sortby(iter, func) → Array of T
Sorts an iterable in ascending order by the result of applying a given function to each element
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to sort |
func |
function(T) |
|
The function to apply to each element |
- Returns
-
Array of T
The sorted array
sorted(iter, compare_fn) → Array of T
Sorts an iterable given a comparison function (same as Array.sort) but not in place
Parameters
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing T |
|
The iterable to sort |
compare_fn |
function(T, T) |
|
The comparison function |
- Returns
-
Array of T
The sorted array
sum(iter) → (number or string)
Sums the elements of an iterable value
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to sum |
- Returns
-
(number or string)
swap(array, index_a, index_b) → Array of T
Swaps the elements at indices index_a
and index_b
in array
. Only works
on arrays so that it can be done in place.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
array |
Array of T |
|
The array to swap elements in |
index_a |
number |
|
The index of the first element to swap |
index_b |
number |
|
The index of the second element to swap |
- Returns
-
Array of T
The resulting array
uniq(iter) → _Set
Creates a new _Set from an iterable. Same as constructors.set.
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to create a set from |
- Returns
zip(...iters) → Array of Array of T
[Global] Zips multiple iterable into a single array of arrays, each containing a single element from each iterable. The result will only be as long as the shortest input.
Examples
zip([1, 2, 3], [4, 5, 6]) // [[1, 4], [2, 5], [3, 6]]
zip([1, 2]) // [[1], [2]]
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iters |
Iterable containing Array of T |
|
The iterables to zip Value can be repeated. |
- Returns
-
Array of Array of T