You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(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.
I understand how to use it not when it would be useful to use.
The Take functions are useful for taking an iterator of unbounded length and providing a stopping condition. It often simplifies both to separate the generate and stop-generating logic, especially since the latter is often involved and specific to a situation.
The Drop functions are useful when you don't need to handle the input until some condition or threshold is passed.
Step only really seems useful for numeric sequences like and even then I can't really think of uses other than what you wrote. Is there a case where you couldn't just put that in the generator? Is there a nonnumeric use case?
Add
(and similarly for the
iter.Seq2
variants) tox/exp/xiter
.t := Take(n, seq)
returns an iteratort
that takes up to the firstn
results ofseq
. Ifseq
yieldsm
values than the iteratort
yieldsmin(m, n)
values.TakeWhile(pred, seq)
is similar toTake
except instead of taking a set number of values it yields whilepred
returnstrue
.Drop(n, seq)
skips the firstn
results ofseq
.DropWhile
similarly skips results while its predicate evaluates to true.Note that
Take{,2}
isLimit{,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}
andSkip{,Func}{,2}
to go withLimit
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
The text was updated successfully, but these errors were encountered: