All Projects → blakeembrey → iterative

blakeembrey / iterative

Licence: other
Functions for working with iterators in JavaScript and TypeScript

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to iterative

iterum
Handling iterables like lazy arrays.
Stars: ✭ 28 (+75%)
Mutual labels:  iterator, iterable
Weihanli.common
common tools,methods,extension methods etc... .net 常用工具类,公共方法,常用扩展方法等,基础类库
Stars: ✭ 152 (+850%)
Mutual labels:  utility, helpers
Swift Algorithms
Commonly used sequence and collection algorithms for Swift
Stars: ✭ 3,934 (+24487.5%)
Mutual labels:  iterator, itertools
betterator
💯 A better sync and async iterator API.
Stars: ✭ 57 (+256.25%)
Mutual labels:  iterator, iterable
asynckit
Minimal async jobs utility library, with streams support
Stars: ✭ 21 (+31.25%)
Mutual labels:  utility, iterator
leak
Show info about package releases on PyPI.
Stars: ✭ 15 (-6.25%)
Mutual labels:  utility, helpers
aimscroll
🍹 Painless utility libary to handle scroll positions and methods in react
Stars: ✭ 12 (-25%)
Mutual labels:  utility
youtube-unofficial
Access parts of your account unavailable through normal YouTube API access.
Stars: ✭ 33 (+106.25%)
Mutual labels:  utility
xcursorlocate
cursor location indicator for x11
Stars: ✭ 16 (+0%)
Mutual labels:  utility
Octotab.crx
⚒ (I'm dead) A super tiny chrome extension making your Github news feed more organized.
Stars: ✭ 17 (+6.25%)
Mutual labels:  utility
actlist-plugin
🔧 Actlist Plugin library to development and debugging.
Stars: ✭ 14 (-12.5%)
Mutual labels:  utility
getify
A utility to grab nested values from objects.
Stars: ✭ 12 (-25%)
Mutual labels:  utility
rhq
Manages your local repositories
Stars: ✭ 48 (+200%)
Mutual labels:  utility
drain-js
Makes smooth transitions between two numbers.
Stars: ✭ 45 (+181.25%)
Mutual labels:  utility
PathCleaner
Cleanup tool for polluted PATH environment variable
Stars: ✭ 18 (+12.5%)
Mutual labels:  utility
DropPoint
Make drag-and-drop easier using DropPoint. Drag content without having to open side-by-side windows
Stars: ✭ 303 (+1793.75%)
Mutual labels:  utility
assign-one-project-github-action
Automatically add an issue or pull request to specific GitHub Project(s) when you create and/or label them.
Stars: ✭ 140 (+775%)
Mutual labels:  utility
fsimilar
find/file similar
Stars: ✭ 13 (-18.75%)
Mutual labels:  utility
useful
🇨🇭 A collection of useful functions for working in Elixir
Stars: ✭ 21 (+31.25%)
Mutual labels:  utility
monoreaper
🌱 Create a monorepo by merging multiple github repositories
Stars: ✭ 21 (+31.25%)
Mutual labels:  utility

Iterative

NPM version NPM downloads Build status Test coverage

Functions for working with iterators in JavaScript, with TypeScript.

(Inspired by itertools)

Installation

npm install iterative --save

Usage

🚨 The packages comes in two flavors, Iterator and AsyncIterator. Use iterative/dist/async for async iterators. 🚨

range(start = 0, stop = Infinity, step = 1): Iterable<number>

This is a versatile function to create lists containing arithmetic progressions.

range(); //=> [0, 1, 2, 3, 4, 5, 6, 7, 8, ...]
range(10, 20, 5); //=> [10, 15]

cycle<T>(iterable: Iterable<T>): Iterable<T>

Make an iterator returning elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely.

cycle([1, 2, 3]); //=> [1, 2, 3, 1, 2, 3, 1, 2, ...]

repeat<T>(value: T, times?: number): Iterable<T>

Make an iterator that repeats value over and over again.

repeat(true); //=> [true, true, true, true, ...]

flatten<T>(iterable: Iterable<Iterable<T>>): Iterable<T>

Return an iterator flattening one level of nesting in an iterable of iterables.

flatten([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
]); //=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

chain<T>(...iterables: Array<Iterable<T>>): Iterable<T>

Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence.

chain([1, 2, 3], [4, 5, 6], [7, 8, 9]); //=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

slice<T>(iterable: Iterable<T>, start = 0, stop = Infinity, step = 1): Iterable<T>

Make an iterator that returns selected elements from the iterable.

slice([1, 2, 3, 4, 5]); //=> [1, 2, 3, 4, 5]
slice(range(), 2, 5); //=> [2, 3, 4]

map<T, U>(iterable: Iterable<T>, func: (x: T) => U): Iterable<U>

Apply function to every item of iterable and return an iterable of the results.

map([1, 2, 3], (x) => x * x); //=> [1, 4, 9]

spreadmap<T, U>(iterable: Iterable<T>, func: (...args: T) => U): Iterable<U>

Make an iterator that computes the function using arguments obtained from the iterable. Used instead of map() when argument parameters are already grouped in tuples from a single iterable (the data has been "pre-zipped"). The difference between map() and spreadmap() parallels the distinction between function(a, b) and function(...c).

map(
  [
    [1, 2],
    [3, 4],
    [5, 6],
  ],
  (a, b) => a + b
); //=> [3, 7, 11]

filter<T, U extends T>(iterable: Iterable<T>, func: Predicate<T, U> = Boolean): Iterable<U>

Construct an iterator from those elements of iterable for which func returns true.

filter(range(0, 10), (x) => x % 2 === 0); //=> [0, 2, 4, 6, 8]

reduce<T, U>(iterable: Iterable<T>, reducer: Reducer<T, U>, initializer?: U): U

Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.

reduce([1, 2, 3], (sum, val) => sum + val); //=> 6

accumulate<T>(iterable: Iterable<T>, func: Reducer<T, T>): Iterable<T>

Make an iterator that returns accumulated results of binary functions.

accumulate([1, 2, 3], (sum, val) => sum + val); //=> [1, 3, 6]

all<T, U extends T>(iterable: Iterable<T>, predicate: Predicate<T, U> = Boolean): boolean

Returns true when all values in iterable are truthy.

all([1, 2, 3], (x) => x % 2 === 0); //=> false

any<T, U extends T>(iterable: Iterable<T>, predicate: Predicate<T, U> = Boolean): boolean

Returns true when any value in iterable is truthy.

any([1, 2, 3], (x) => x % 2 === 0); //=> true

contains<T>(iterable: Iterable<T>, needle: T): boolean

Returns true when any value in iterable is equal to needle.

contains("test", "t"); //=> true

dropWhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>

Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element.

dropWhile([1, 2, 3, 4, 5], (x) => x < 3); //=> [3, 4, 5]

takeWhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>

Make an iterator that returns elements from the iterable as long as the predicate is true.

takeWhile([1, 2, 3, 4, 5], (x) => x < 3); //=> [1, 2]

groupBy<T, U>(iterable: Iterable<T>, func: (x: T) => U): Iterable<[U, Iterable<T>]>

Make an iterator that returns consecutive keys and groups from the iterable. The func is a function computing a key value for each element.

groupBy(range(0, 6), (x) => Math.floor(x / 2)); //=> [[0, [0, 1]], [1, [2, 3]], [2, [4, 5]]]

enumerate<T>(iterable: Iterable<T>, offset = 0): Iterable<[number, T]>

Returns an iterable of enumeration pairs.

enumerate("test"); //=> [[0, 't'], [1, 'e'], [2, 's'], [3, 't']]

zip<T>(...iterables: Iterable<T>[]): Iterable<T[]>

Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted.

zip([1, 2, 3], ["a", "b", "c"]); //=> [[1, 'a'], [2, 'b'], [3, 'c']]

zipLongest<T>(...iterables: Iterable<T>[]): Iterable<(T | undefined)[]>

Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are undefined. Iteration continues until the longest iterable is exhausted.

zipLongest([1, 2], ["a", "b", "c", "d"]); //=> [[1, 'a'], [2, 'b'], [undefined, 'c'], [undefined, 'd']]

zipWithValue<T, U>(fillValue: U, iterables: Iterable<T>[]): Iterable<(T | U)[]>

Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are fillValue. Iteration continues until the longest iterable is exhausted.

zipWithValue("example", [1, 2], ["a", "b", "c", "d"]); //=> [[1, 'a'], [2, 'b'], ['example', 'c'], ['example', 'd']]

tee<T>(iterable: Iterable<T>): [Iterable<T>, Iterable<T>]

Return two independent iterables from a single iterable.

tee([1, 2, 3]); //=> [[1, 2, 3], [1, 2, 3]]

chunk<T>(iterable: Iterable<T>, size: number): Iterable<T[]>

Break iterable into lists of length size.

chunk(range(0, 10), 2); //=> [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]

pairwise<T>(iterable: Iterable<T>): Iterable<[T, T]>

Returns an iterator of paired items, overlapping, from the original. When the input iterable has a finite number of items n, the outputted iterable will have n - 1 items.

pairwise(range(0, 5)); //=> [[0, 1], [1, 2], [2, 3], [3, 4]]

compress<T>(iterable: Iterable<T>, selectors: Iterable<boolean>): Iterable<T>

Make an iterator that filters elements from iterable returning only those that have a corresponding element in selectors that evaluates to true.

compress([1, 2, 3, 4, 5], [true, false, true, false, true]); //=> [1, 3, 5]

sorted<T, U>(iterable: Iterable<T>, key: (x: T) => U, cmp: (x: U, y: U) => number, reverse?: boolean): T[]

Return a sorted array from the items in iterable.

sorted(slice(range(), 0, 10), (x) => x);

list<T>(iterable: Iterable<T>): T[]

Creates an array from an iterable object.

list(range(0, 5)); //=> [0, 1, 2, 3, 4]

dict<K, V>(iterable: Iterable<[K, V]>): Record<K, V>

Return an object from an iterable, i.e. Array.from for objects.

dict(zip(range(0, 5), repeat(true))); //=> { 0: true, 1: true, 2: true, 3: true, 4: true }

len(iterable: Iterable<any>): number

Return the length (the number of items) of an iterable.

len(range(0, 5)); //=> 5

Note: This method iterates over iterable to return the length.

min<T>(iterable: Iterable<T>, key?: (x: T) => number): T

Return the smallest item in an iterable.

min([1, 2, 3, 4, 5]); //=> 1

max<T>(iterable: Iterable<T>, key?: (x: T) => number): T

Return the largest item in an iterable.

max([1, 2, 3, 4, 5]); //=> 5

sum(iterable: Iterable<number>, start?: number): number

Sums start and the items of an iterable from left to right and returns the total.

sum([1, 2, 3, 4, 5]); //=> 15

product<T>(...iterables: Iterable<T>[]): Iterable<T[]>

Cartesian product of input iterables.

product("ABCD", "xy"); //=> Ax Ay Bx By Cx Cy Dx Dy

Reference

TypeScript

This project uses TypeScript and publishes definitions on NPM.

License

Apache 2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].