Iterama
Composable functional Iterable<T>
helpers.
Install
npm install iterama
concat
<T> (...iterables: Iterable<T>[]) => Iterable<T>
import { concat } from 'iterama'
const data0 = [1, 2, 3],
data1 = [4, 5, 6]
const result = [
...concat(data0, data1)
]
distinct
<T> (iterable: Iterable<T>) => Iterable<T>
import { distinct } from 'iterama'
const result = [
...distinct([1, 1, 3, 3, 4, 3])
]
filter
<T> (predicate: (arg: T) => boolean) => (iterable: Iterable<T>) => Iterable<T>
import { filter } from 'iterama'
const isEven = (x: number) => x % 2 === 0
const result = [
...filter(isEven)([1, 2, 3, 4])
]
filterEx
<T> (predicate: (arg: T, index: number, it: Iterable<T>) => boolean) => (iterable: Iterable<T>) => Iterable<T>
import { filterEx } from 'iterama'
const skipEverySecond = (x: string, i: number) => i % 2 !== 0
const result = [
...filterEx(skipEverySecond)(['a', 'b', 'c', 'd'])
]
iterate
<T> (iterable: Iterable<T>) => IterableIterator<T>
import { iterate } from 'iterama'
const result = [
...iterate([1, 2, 3, 4, 5])
]
length
(maxLength: number) => <T> (iterable: Iterable<T>) => number
import { length } from 'iterama'
const l = length(Number.MAX_SAFE_INTEGER)
const result = l([1, 2, 3, 4, 5])
map
<T, R> (xf: (arg: T) => R) => (iterable: Iterable<T>) => Iterable<R>
import { map } from 'iterama'
const mult2 = (x: number) => x * 2
const result = [
...map(mult2)([1, 2, 3, 4])
]
mapEx
<T, R> (xf: (arg: T, i: number, it: Iterable<T>) => R) => (iterable: Iterable<T>) => Iterable<R>
import { mapEx } from 'iterama'
const addIndex = (x: number, i: number) => x + i
const result = [
...mapEx(addIndex)([1, 2, 3, 4])
]
range
(length: number) => Iterable<number>
import { range } from 'iterama'
const result = [
...range(4)
]
reduce
<T, R> (reducer: (a: R, b: T) => R) => (iterable: Iterable<T>) => Iterable<R>
import { reduce } from 'iterama'
const reducer = (state: number = 0, b: number = 0) => a + b
const result = [
...reduce(reducer)([1, 2, 3, 4])
]
reduceEx
<T, R> (reducer: (a: R, b: T, i: number, it: Iterable<T>) => R, initial: R) => (iterable: Iterable<T>) => Iterable<R>
import { reduceEx } from 'iterama'
const reducer = (acc: number, val: number, i: number) => acc + val
const result = [
...reduceEx(reducer, 0)([1, 2, 3, 4])
]
scan
<T, R> (reducer: (a: R, b: T) => R) => (iterable: Iterable<T>) => Iterable<R>
import { scan } from 'iterama'
const reducer = (state: number = 0, b: number = 0) => a + b
const result = [
...scan(reducer)([1, 2, 3, 4])
]
scanEx
<T, R> (reducer: (a: R, b: T, i: number, it: Iterable<T>) => R) => (iterable: Iterable<T>) => Iterable<R>
import { scanEx } from 'iterama'
const reducer = (acc: number, val: number, i: number) => acc + val
const result = [
...scanEx(reducer, 0)([1, 2, 3, 4])
]
skip
(n: number) => <T> (iterable: Iterable<T>) => Iterable<T>
import { skip } from 'iterama'
const result0 = [
...skip(2)([1, 2, 3, 4, 5, 6])
]
const result1 = [
...skip(-2)([1, 2, 3, 4, 5, 6])
]
slice
(skip: number, take: number) => <T> (it: Iterable<T>): Iterable<T>
import { slice } from 'iterama'
const r0 = [
...slice(1, 2)([1, 2, 3, 4, 5])
]
const r1 = [
...slice(-2, 1)([1, 2, 3, 4, 5])
]
const r2 = [
...slice(0, -2)([1, 2, 3, 4, 5])
]
const r3 = [
...slice(2)([1, 2, 3, 4, 5])
]
const r4 = [
...slice(-2)([1, 2, 3, 4, 5])
]
const r5 = [
...slice()([1, 2, 3, 4, 5])
]
startWith
<T> (value: T) => (iterable: Iterable<T>)
import { startWith } from 'iterama'
const r = [
...startWith(0)([1, 2, 3])
]
take
(n: number) => <T> (iterable: Iterable<T>) => Iterable<T>
import { take } from 'iterama'
const r0 = [
...take(2)([1, 2, 3, 4, 5])
]
const r1 = [
...take(-2)([1, 2, 3, 4, 5])
]
unique
<T> (iterable: Iterable<T>) => Iterable<T>
import { unique } from 'iterama'
const r = [
...unique([1, 1, 3, 4, 3])
]
zip
<A, B> (it0: Iterable<A>, it1: Iterable<B>): Iterable<[A, B]>
<A, B, C> (it0: Iterable<A>, it1: Iterable<B>, it2: Iterable<C>): Iterable<[A, B, C]>
<A, B, C, D> (it0: Iterable<A>, it1: Iterable<B>, it2: Iterable<C>, it3: Iterable<C>): Iterable<[A, B, C, D]>
import { zip } from 'iterama'
const r = [
...zip([1, 2, 3, 4, 5, 6], ['a', 'b', 'c', 'd'])
]