scala-library-next icon indicating copy to clipboard operation
scala-library-next copied to clipboard

Add `takeUntil(p: A => Boolean)` to scala.collection.GenTraversableLike

Open ronanM opened this issue 8 years ago • 12 comments

List(1, 2, 3, 4).takeWhile(_ != 3) // List(1, 2)

List(1, 2, 3, 4).takeUntil(_ == 3) // List(1, 2, 3)

ronanM avatar Aug 20 '17 16:08 ronanM

This naming is directly in conflict with 0 until 5, which excludes the endpoint. The functionality is helpful, but it should be called takeTo or somesuch. (Or maybe takeWhile should be deprecated in favor of takeUntil and takeTo.)

Ichoran avatar Aug 21 '17 20:08 Ichoran

It's named like that in some other libs, RxJava for exemple:

takeWhile: takeWhile

takeUntil: takeUntil

ronanM avatar Aug 21 '17 20:08 ronanM

@ronanM - The do/until vs while constructs of C set a precedent also, but that isn't how Scala ranges work, and hence, isn't how things should be named in Scala.

Ichoran avatar Aug 21 '17 20:08 Ichoran

Anyway, I think it's oft-missed functionality, so it should go in under some name.

Ichoran avatar Aug 21 '17 20:08 Ichoran

If this goes in, it should come with similar drop and span methods. The problem with those, though, is that if you get an empty list for the drop, you don't know if it happened because the last element satisfied the predicate, or because no element satisfied the predicate :(

sjrd avatar Aug 21 '17 21:08 sjrd

if you get an empty list for the drop, you don't know if it happened because the last element satisfied the predicate, or because no element satisfied the predicate :(

Is it a real problem ?

ronanM avatar Aug 21 '17 21:08 ronanM

I don't know. Just pointing it out.

sjrd avatar Aug 21 '17 21:08 sjrd

Ok, thank you ;-)

ronanM avatar Aug 21 '17 21:08 ronanM

Similarly proposed awhile back in #2963.

Would like to see this revisited if possible.

JMdoubleU avatar May 15 '20 22:05 JMdoubleU

as per https://github.com/scala/scala-dev/issues/661 the earliest we could add it is Scala 3.1 which is a ways off, but in the meantime, but it could go in to https://github.com/scala/scala-collection-contrib

SethTisue avatar May 15 '20 23:05 SethTisue

This would make things like this easier:

def toIterator(st: java.util.StringTokenizer): Iterator[String] =
   Iterator.continually(st.nextToken()).takeUntil(_ => st.hasMoreTokens()))

pathikrit avatar Sep 09 '22 13:09 pathikrit

This thing is really useful. Please reconsider the pending state.

shestero avatar Oct 19 '22 06:10 shestero