Skip to content

tail(0, seq) returns the whole sequence instead of empty (and is inconsistent across iterable types) #626

Description

@pramodavansaber

Summary

tail(n, seq) is implemented as seq[-n:] (with a deque fallback). For n == 0, seq[-0:] is seq[0:] — the entire sequence — so tail(0, seq) returns everything instead of the last zero elements. It is also inconsistent across input types: sliceable inputs return the whole sequence, while non-sliceable iterables hit the deque(seq, 0) fallback and correctly return empty.

Reproduction

from toolz import tail
print(tail(0, [10, 20, 30]))        # [10, 20, 30]   <- expected () / empty
print(tuple(tail(0, iter([10, 20, 30]))))   # ()      (deque fallback: empty)

Expected

tail(0, seq) returns an empty result (the last zero elements), consistently for any iterable.

Actual

Returns the whole sequence for sliceable inputs and empty for non-sliceable ones — inconsistent, and wrong for n == 0.

Fix sketch

Special-case n <= 0 to return empty, or route through the deque path uniformly.

Environment

toolz 1.1.0 (master @ 568c2b8), Python 3.12.

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