scala-library-next
scala-library-next copied to clipboard
Add `takeUntil(p: A => Boolean)` to scala.collection.GenTraversableLike
List(1, 2, 3, 4).takeWhile(_ != 3) // List(1, 2)
List(1, 2, 3, 4).takeUntil(_ == 3) // List(1, 2, 3)
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.)
It's named like that in some other libs, RxJava for exemple:
takeWhile:

takeUntil:

@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.
Anyway, I think it's oft-missed functionality, so it should go in under some name.
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 :(
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 ?
I don't know. Just pointing it out.
Ok, thank you ;-)
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
This would make things like this easier:
def toIterator(st: java.util.StringTokenizer): Iterator[String] =
Iterator.continually(st.nextToken()).takeUntil(_ => st.hasMoreTokens()))
This thing is really useful. Please reconsider the pending state.