Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "Iter"

Index

Functions

combinationsOf

  • combinationsOf<T>(arr: T[], n: number): IterableIterator<T[]>
  • A generator which yields all of the combinations of N items from an array.

    import { combinationsOf } from 'goodish'
    console.log(Array.from(combinationsOf([0, 1, 2], 2)) // [[0, 1], [0, 2], [1, 2]]
    reference

    Copied verbatim from this site

    Type parameters

    • T

    Parameters

    • arr: T[]
    • n: number

    Returns IterableIterator<T[]>

count

  • count(arr: Iterable<any>): number
  • Count the number of items in an iterator

    import { count } from 'goodish'
    console.log(count([0, 1, 2, 3, 4])) // 5

    Parameters

    • arr: Iterable<any>

    Returns number

permutationsOf

  • permutationsOf<T>(arr: T[]): IterableIterator<T[]>
  • A generator which yields all of the permutations of an array

    import { permutationsOf } from 'goodish'
    console.log(Array.from(permutationsOf([0, 1, 2, 3, 4]))
    reference

    Adapted from this answer

    Type parameters

    • T

    Parameters

    • arr: T[]

    Returns IterableIterator<T[]>

randomFrom

  • randomFrom<T>(arr: T[]): [T, number]
  • Returns a tuple containing a random element from a collection and the index of that element in the collection.

    import { randomFrom, setSeed } from 'goodish'
    const arr = [0, 1, 2, 3, 4]
    console.log(randomFrom(arr)) // any single element of arr. will be different each time this code runs
    setSeed(1)
    console.log(randomFrom(arr)) // always the same element of arr

    Type parameters

    • T

    Parameters

    • arr: T[]

    Returns [T, number]

randomNFrom

  • randomNFrom<T>(arr: T[], n: number): T[]
  • Returns an array of N randomly selected elements from the supplied array

    import { randomNFrom, setSeed } from 'goodish'
    const arr = 'abcdefghijklmnopqrstuvwxyz0123456789'
    console.log(randomNFrom(arr, 10)) // a random alphanumeric array with 10 characters
    setSeed(1)
    console.log(randomNFrom(arr, 10).join('')) // always the same alphanumeric string

    Type parameters

    • T

    Parameters

    • arr: T[]
    • n: number

    Returns T[]

range

  • range(min: number, max: number, step?: number): IterableIterator<number>
  • A generator which yields each value between the min and max values

    import { range } from 'goodish'
    for (const n of range(0, 5)) {
      console.log(n) // 0, 1, 2, 3, 4
    }

    Parameters

    • min: number
    • max: number
    • Default value step: number = 1

    Returns IterableIterator<number>

rangeArr

  • rangeArr(min: number, max: number, step?: number): number[]
  • Returns an array of values between the min and max values.

    import { rangeArr } from 'goodish'
    const arr = rangeArr(0, 5) // [0, 1, 2, 3, 4, 5]

    Parameters

    • min: number
    • max: number
    • Default value step: number = 1

    Returns number[]

shuffle

  • shuffle<T>(arr: T[]): T[]
  • Shuffle an array in place.

    import { shuffle, setSeed } from 'goodish'
    const arr = [0, 1, 2, 3, 4]
    console.log(shuffle(arr.slice())) // a different array order each time this code is run
    setSeed(1)
    console.log(shuffle(arr.slice()) // a different array order, but will be the same each time the code is run
    reference

    Adapted from here

    Type parameters

    • T

    Parameters

    • arr: T[]

    Returns T[]

swap

  • swap<T>(arr: T[], indexA: number, indexB: number): T[]
  • Swap two indexes of an array

    import { swap } from 'goodish'
    const arr = [0, 1, 2]
    swap(1, 2)
    console.log(arr) // [0, 2, 1]

    Type parameters

    • T

    Parameters

    • arr: T[]
    • indexA: number
    • indexB: number

    Returns T[]

transpose

  • transpose<T>(arr: T[][]): T[][]
  • Transpose a 2D array.

    import { transpose } from 'goodish'
    console.log(transpose([[0], [1], [2]])) // [[0, 1, 2]]
    reference

    Copied verbatim from this answer

    Type parameters

    • T

    Parameters

    • arr: T[][]

    Returns T[][]

Generated using TypeDoc