-
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)- returnstrueif an object is iterable. -
isArray(obj)- returnstrueif 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 anitemto the end ofiter. -
at(index, iter)- returns Option,Someif there is a value at the index andNoneif 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,Somecontaining the average value of an iterable full of numbers andNoneif the iterable is empty. -
chain(fn, iter)- flattens an iterable, then maps its elements according tofn. -
compact(iter)- removes allnull,undefined, andNaNvalues 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)- runsfnon 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, orNoneif it is not found. Pred can be a predicate function, a RegExp, or any value (including objects). -
first(iter)- returns an Option,Someof 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 fromindexto the end. -
get(index)- alias forat. -
includes(value, iter)- Returns true ifitercontainsvalue. Works with any value, including objects. -
indexOf(iter, value[, start])- returns Option,Someof the first index ofvalueif it is found, otherwiseNone. -
insert(item, index, iter)- insertsitematindexofiter -
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)- returnstrueif 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, callingtoStringon each element, separating elements bysep. -
keys(iter)- returns the keys of an iterable -
last(iter)- returns an Option,Someof the last value in an iterator orNoneif 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,Someof the max value of an iterable full of numbers orNoneif it is empty. -
median(iter)- returnsSomeof the median value of an iterable full of numbers orNoneif the iterable is empty. -
min(iter)- returnsSomeof the min value of an iterable full of numbers orNoneif it is empty. -
none(search, iter)- returnstrueif 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 firstnumItemsitems initer. -
pop(iter)- alias forlast. -
prepend(item, iter)- prependsitemto the front ofiter. -
product(iter)- returns Option,Someof all the values of an iterable full of numbers multiplied together,Noneif 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 matchsearchfrom the iterable.searchcan 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 theendvalue.stepdefaults 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 akeyin 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 ofkeyorfn. If you want the sort order reversed, setreversedtotrue. -
splice(iter[, start, deleteCount, ...items])- insert or remove items from an iterable.startanddeleteCountdefault to 0. Providing one or moreitemswill cause them to be inserted atstart. IfdeleteCountis set to something other than 0, that many items will be removed from the returned iterable. -
removeAt(iter, start[, end])- removes items fromiterstarting at indexstartand going untilend(non-inclusive) or the end of the iterable. -
sum(iter)- returns Option,Someof the value of all numbers added together orNonefor 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 ofitertoindex(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 functionupdaterto update the value ofiteratindex. Ifindexis not initerthe 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 havenullvalues. Use only if you know all your iterables are the same length.