• Get the first element from an iterable.

    Time complexity: O(1)

    Space complexity: O(1)

    Type Parameters

    • T

    Parameters

    • iterable: Iterable<T>

      The iterable to take elements from

    • Optionaltake: undefined

    Returns T | undefined

    The first element of the iterable

    Iterable

    first([1, 2, 3]); // 1
    
  • Get the first n elements from an iterable.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • T

    Parameters

    • iterable: Iterable<T>

      The iterable to take elements from

    • take: number

      The number of elements to take from the iterable

    Returns Iterable<T>

    The first take elements of the iterable

    Iterable

    [...first([1, 2, 3], 1)]; // [1]
    
    [...first([1, 2, 3], 2)]; // [1, 2]