Skip to content

proposal: x/exp/xiter: add {Drop,Take}{,While}{,2}Β #67442

@jimmyfrasche

Description

@jimmyfrasche

Add

func Drop[K any](int, iter.Seq[K]) iter.Seq[K]

func DropWhile[K any](func(K) bool, iter.Seq[K]) iter.Seq[K]

func Take[K any](int, iter.Seq[K]) iter.Seq[K]

func TakeWhile[K any](func(K) bool, iter.Seq[K]) iter.Seq[K]

(and similarly for the iter.Seq2 variants) to x/exp/xiter.

t := Take(n, seq) returns an iterator t that takes up to the first n results of seq. If seq yields m values than the iterator t yields min(m, n) values.

TakeWhile(pred, seq) is similar to Take except instead of taking a set number of values it yields while pred returns true.

Drop(n, seq) skips the first n results of seq. DropWhile similarly skips results while its predicate evaluates to true.


Note that Take{,2} is Limit{,2} in #61898. This proposal thus includes renaming that.

These names and functions are standard across all languages I checked (Python, Haskell, OCaml, Scheme). I had originally proposed these as LimitFunc{,2} and Skip{,Func}{,2} to go with Limit but decided to stick with the common names in this proposal.

While there is an argument to be made that TakeFunc etc. would be more in line with Go naming, but, in addition, to bucking the trend from other languages' libraries that doesn't say which way the predicate is used whereas While makes it clear.

Here is a link with basic examples and implementations https://go.dev/play/p/DQhz7X_95wf?v=gotip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions