Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Surprising/unintuitive difference in behaviour involving multiple materialized take calls #296

@noinkling

Description

@noinkling
const nums = [1,2,3,4,5,6,7,8,9]

const iter1 = nums.values()
iter1.take(2).toArray() // [1, 2]
iter1.take(2).toArray() // [3, 4]
iter1.take(2).toArray() // [5, 6]
iter1.next() // {value: 7, done: false}

const iter2 = nums.values().map(n => n) // pretend the mapping function is doing something useful
iter2.take(2).toArray() // [1, 2]
iter2.take(2).toArray() // []
iter2.next() // {value: undefined, done: true}

At the intuitive level, I don't expect these to behave differently, it feels like an arbitrary distinction in this case.

The alternative of repeating the map (and/or flatMap, filter) call feels clunky, even if it's most likely to be done in a loop:

iter1.map(n => n).take(2).toArray()
// or
iter1.take(2).map(n => n).toArray()

With the chunking proposal in the works maybe this particular case isn't very compelling, but I thought I'd bring it up anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions