-
Notifications
You must be signed in to change notification settings - Fork 0
Functions Iterable
Jason Barr edited this page Sep 22, 2021
·
1 revision
The iterable functions here will work with Arrays or any of the iterable collection types in this library. They will also work with any custom iterable that defines a constructor
property that will work without using new
.
These functions are also used as the basis of the methods used by the iterable collections.
All of these functions return a new value. None of them will mutate the original iterable.
All functions that take multiple arguments and the iterable last are auto-curried, so you can generate a new function using partial application.
Example:
const double = map(x => x * 2); // will double all the numbers in an iterable
-
isIterable(obj)
- returnstrue
if an object is iterable. -
isArray(obj)
- returnstrue
if object is an array. -
all(search, iter)
- search can be a function, RegExp, or any value (including objects). If every item in the iterable passes, returns true. -
any(search, iter)
- search can be a function, RegExp, or any value (including objects). If any item in the iterable passes, returns true. -
ap(other, iter)
- applies all the functions in an iterable of functions to all the items ofiter
. -
append(item, iter)
- adds anitem
to the end ofiter
. -
at(index, iter)
- returns Option,Some
if there is a value at the index andNone
if there is not. -
atUnsafe(index, iter)
- likeat
, but unwraps the value from the Option. May return a null or undefined value. -
average(iter)
- returns Option,Some
containing the average value of an iterable full of numbers andNone
if the iterable is empty. -
chain(fn, iter)
- flattens an iterable, then maps its elements according tofn
. -
compact(iter)
- removes allnull
,undefined
, andNaN
values from an iterable. -
concat(...iters)
- concatenates iterables into a single iterable of the first argument's type. -
concatToArray(...iters)
- concatenates iterables into a single Array. -
copyWithin(iter, target[, start, end])
- shallow copies part of an iterable to another location in the same iterable and returns it without modifying its length. -
count(search, iter)
- search can be a function, RegExp, or any value (including objects). Returns the count of items in the iterable that satisfysearch
. -
difference(iter1, iter2)
- returns an iterable containing the difference between the first and second arguments. -
each(fn, iter)
- runsfn
on each item in the iterable. -
eachWithIndex(fn, iter)
- same aseach
, but the callback takes both the item and its index. -
entries(iter)
- returns an Array of Arrays, each containing a pair of the item's index and the item itself. -
exclude(pred, iter)
- alias forreject
-
find(pred, iter)
- finds an item in the iterable based on the predicate function. Returns an Option, in case the item is not found. Pred can be a predicate function, a RegExp, or any value (including objects). -
findIndex(pred, iter)
- returns an Option containing the index of the found item, orNone
if it is not found. Pred can be a predicate function, a RegExp, or any value (including objects). -
first(iter)
- returns an Option,Some
of the first value in the iterable if it is not empty, otherwiseNone
. -
flatten(iter)
- flattens an iterable of iterables into a single iterable of the container's type. -
flatMap(fn, iter)
- alias forchain
. -
fold(fn, initial, iter)
- alias forreduce
. -
foldLeft(fn, initial, iter)
- alias forreduce
. -
foldRight(fn, initial, iter)
- alias forreduceRight
. -
forEach(fn, iter)
- alias foreachWithIndex
. -
from(index, iter)
- returns a slice of the iterable fromindex
to the end. -
get(index)
- alias forat
. -
includes(value, iter)
- Returns true ifiter
containsvalue
. Works with any value, including objects. -
indexOf(iter, value[, start])
- returns Option,Some
of the first index ofvalue
if it is found, otherwiseNone
. -
insert(item, index, iter)
- insertsitem
atindex
ofiter
-
intersection(iter1, iter2)
- returns an iterable containing the intersection of the two iterables. -
isEmpty(iter)
- returns true if an iterator contains no values. -
isEqual(iter1, iter2)
- returnstrue
if both iterables contain the same values. Checks objects by value (deep equality), not by reference. -
join(sep, iter)
- joins all the values in an iterator into a string, callingtoString
on each element, separating elements bysep
. -
keys(iter)
- returns the keys of an iterable -
last(iter)
- returns an Option,Some
of the last value in an iterator orNone
if it is empty. -
length(iter)
- returns the length of an iterator. -
map(fn, iter)
- returns a new iterator of the same type with values mapped from the original usingfn
. -
mapWithIndex(fn, iter)
- same asmap
, but also passes the index number into the mapping function. -
max(iter)
- returns Option,Some
of the max value of an iterable full of numbers orNone
if it is empty. -
median(iter)
- returnsSome
of the median value of an iterable full of numbers orNone
if the iterable is empty. -
min(iter)
- returnsSome
of the min value of an iterable full of numbers orNone
if it is empty. -
none(search, iter)
- returnstrue
if none of the items in the array matchsearch
, which can be a function, RegExp, or any value (including an object). -
pluck(numItems, iter)
- returns an iterable of the firstnumItems
items initer
. -
pop(iter)
- alias forlast
. -
prepend(item, iter)
- prependsitem
to the front ofiter
. -
product(iter)
- returns Option,Some
of all the values of an iterable full of numbers multiplied together,None
if it is empty. -
push(item, iter)
- alias forappend
. -
reduce(fn, initial, iter)
- generates a single value from the values in the iterable. Must supply initial value in addition to reducer function. -
reduceRight(fn, initial, iter)
- same asreduce
, but traverses the iterable backwards. -
reject(fn, iter)
- the opposite offilter
- values that pass the predicate will be removed from the result. -
remove(search, iter)
- removes all items that matchsearch
from the iterable.search
can be a function, RegExp, or any value (including an object). -
reverse(iter)
- reverses an iterable. -
sample(iter)
- returns a random value from the iterable. -
shift(iter)
- alias forfirst
. -
shuffle(iter)
- uses the Fisher-Yates algorithm to shuffle an iterable. -
slice(iter, [start,] end[, step])
- Python-style slicing. If you pass an iterable and a single argument it will take that as theend
value.step
defaults to 1. Can use negative indices and a negative step value to reverse the slice. -
some(search, iter)
- alias forany
. -
sort(iter[, { key, fn, reversed }])
- returns a sorted iterable. If you provide no additional arguments, it will properly sort numbers, strings, or booleans (unlike the native JavaScriptArray.prototype.sort
). If you provide akey
in the options object, it will sort objects based on the value of that key (again, properly sorting numbers, strings, and booleans). If you need more control, provide a function forfn
. Only use one ofkey
orfn
. If you want the sort order reversed, setreversed
totrue
. -
splice(iter[, start, deleteCount, ...items])
- insert or remove items from an iterable.start
anddeleteCount
default to 0. Providing one or moreitems
will cause them to be inserted atstart
. IfdeleteCount
is set to something other than 0, that many items will be removed from the returned iterable. -
removeAt(iter, start[, end])
- removes items fromiter
starting at indexstart
and going untilend
(non-inclusive) or the end of the iterable. -
sum(iter)
- returns Option,Some
of the value of all numbers added together orNone
for an empty iterable. -
symmetricDifference(iter1, iter2)
- returns a new iterable containing the symmetric difference of its arguments. -
take(index, iter)
- alias forpluck
. -
to(index, iter)
- returns a new iterable containing the items from the beginning ofiter
toindex
(non-inclusive). -
toArray(iter)
- converts any iterable to an Array. -
union(iter1, iter2)
- returns the union of its arguments. -
unique(iter)
- strips out duplicate entries from an iterable. -
unshift(iter)
- alias forprepend
. -
update(updater, index, iter)
- uses the functionupdater
to update the value ofiter
atindex
. Ifindex
is not initer
the iterable will be returned unchanged. -
values(iter)
- returns an array of the values of an iterable. -
zip(iters)
- zips a series of iterators; for example,zip([1, 2, 3], ["a", "b", "c"])
returns[[1, "a"], [2, "b"], [3, "c"]]
. This method is unsafe, as you may havenull
values. Use only if you know all your iterables are the same length.