A TypeScript library for working with iterables and generators, providing a rich set of utility functions for functional programming patterns.
npm i -E @prelude/generator
import * as G from '@prelude/generator'
console.log(G.pipe1(
G.charRange('a', 'c'),
G.cycle,
G.take(10),
G.array
))
// [
// 'a', 'b', 'c', 'a',
// 'b', 'c', 'a', 'b',
// 'c', 'a'
// ]
append
- Creates a generator that yields all values from the source iterable followed by all values from appendValues.areUnique
- Checks if all elements in the iterable are unique according to their values or a derived key.array
- Converts an iterable to an array.at
- Retrieves the element at the specified index in an iterable.batch
- Creates a generator that yields arrays of values from the source iterable, grouped into batches of specified size.bigproduct
- Multiplies all numeric values in an iterable using BigInt for precise large number arithmetic.bigsum
- Sums all numeric values in an iterable using BigInt for precise large number arithmetic.cartesianProduct
- Creates a generator that yields all possible combinations of elements from multiple iterables.charRange
- Creates a generator that yields characters in a range from start to end character.combinations
- Creates a generator that yields all possible combinations of a specific length from an iterable.compact
- Creates a generator that yields all non-nullish values from the source iterable.composites
- Creates a generator that yields composite numbers (non-prime numbers greater than 1).concat
- Creates a generator that yields all values from multiple iterables in sequence.consume
- Exhausts an iterable by iterating through all its values without returning anything.count
- Returns the number of elements in an iterable.cycle
- Creates a generator that yields the elements of an iterable repeatedly, cycling through them indefinitely.defined
- Creates a generator that yields only the defined values from the source iterable.diff
- Creates a generator that yields elements from the first iterable that are not present in the second iterable.every
- Checks if all elements in an iterable satisfy a given predicate.extend
- Creates a generator that yields elements from the source followed by a repeating value until a specified length is reached.extreme
- Finds the extreme (min/max) value in an iterable based on a comparison function.fibonacci
- Creates a generator that yields the Fibonacci sequence.filter
- Creates a generator that yields only elements from an iterable that pass a predicate test.findIndex
- Returns the index of the first element in an iterable that satisfies a predicate.find
- Returns the first element in an iterable that satisfies a predicate, or throws if none found.first
- Returns the first element of an iterable, or throws if the iterable is empty.flatMap
- Creates a generator that maps each element using a function and flattens the result.flatten
- Creates a generator that flattens nested iterables into a single level.fork
- Creates multiple independent iterators from a single iterable source.from
- Creates a generator from various input types.generator
- Creates a stateful generator function from an initialization function.groupMap
- Groups elements of an iterable by keys derived from a mapping function.groupObject
- Groups elements of an iterable by keys derived from a mapping function into an object.group
- Creates a generator that yields arrays of consecutive elements that share the same key.interleave
- Creates a generator that alternates values from multiple iterables.joinStrings
- Joins string values from an iterable with a separator.last
- Returns the last element of an iterable, or throws if the iterable is empty.length
- Returns the number of elements in an iterable.lift
- Converts a function that works on values to one that works on iterables.map
- Creates a generator that transforms each value in an iterable using a mapping function.max
- Returns the maximum value in an iterable according to a comparison function.maybeFind
- Returns the first element in an iterable that satisfies a predicate, or undefined if none found.maybeFirst
- Returns the first element of an iterable, or undefined if the iterable is empty.maybeLast
- Returns the last element of an iterable, or undefined if the iterable is empty.maybeSingle
- Returns the single element if the iterable contains exactly one element, or undefined otherwise.memoized
- Creates a generator that caches results of an input generator for reuse.min
- Returns the minimum value in an iterable according to a comparison function.of
- Creates a generator that yields the provided values.pair
- Creates a generator that yields pairs of adjacent elements from an iterable.permutations
- Creates a generator that yields all possible arrangements of elements from an iterable.pipe
- Passes a value through a series of transformations.pipe0
- Creates a pipeline of transformations to be applied to a value later.prelude
- Exports helpers for integrating with @prelude libraries.prepend
- Creates a generator that yields values from prependValues followed by all values from the source iterable.primeFactors
- Creates a generator that yields all prime factors of a number.primes
- Creates a generator that yields prime numbers.product
- Multiplies all numeric values in an iterable.properDivisors
- Creates a generator that yields all proper divisors of a number.range
- Creates a generator that yields numbers in a sequence from start to end.record
- Converts an iterable of key-value pairs into a record object.recursive
- Creates a generator from a recursive function definition.reduce
- Applies a reducer function to each element in an iterable, resulting in a single output value.reduce1
- Applies a reducer function to elements in a non-empty iterable, using the first element as the initial value.repeat
- Creates a generator that yields a value repeatedly.retain
- Creates a generator that keeps only elements that are present in both iterables.reverse
- Creates a generator that yields elements from an iterable in reverse order.rotate
- Creates a generator that rotates elements of an iterable by a specified offset.single
- Returns the single element if the iterable contains exactly one element, or throws otherwise.skipWhile
- Creates a generator that skips elements from an iterable as long as a predicate returns true.skip
- Creates a generator that skips a specified number of elements from the beginning of an iterable.some
- Checks if at least one element in an iterable satisfies a given predicate.sort
- Creates a generator that yields elements from an iterable in sorted order.sortedDiff
- Creates a generator that yields elements from the first sorted iterable that are not present in the second sorted iterable.step
- Creates a generator that yields every nth element from an iterable.sum
- Sums all numeric values in an iterable.take
- Creates a generator that yields a specified number of elements from the beginning of an iterable.tap
- Creates a generator that applies a side-effect function to each element while yielding the original values.unique
- Creates a generator that yields only unique values from an iterable.unsafeCombinations
- Creates a generator that yields all possible combinations of a specific length from an array.unsafePermutations
- Creates a generator that yields all possible arrangements of elements from an array.until
- Creates a generator that yields elements from an iterable until a predicate returns true.while
- Creates a generator that yields elements from an iterable as long as a predicate returns true.window
- Creates a generator that yields sliding windows of elements from an iterable.withIndex
- Creates a generator that yields pairs of [index, value] for each element in an iterable.yield
- Creates a generator that yields a single value.zipRecord
- Creates a generator that combines elements from iterables into record objects using provided keys.zip
- Creates a generator that combines corresponding elements from multiple iterables.
MIT License
Copyright 2021 Mirek Rusin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.