Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add takeTo to Traversable #2963

Closed
scabug opened this issue Jan 25, 2010 · 9 comments
Closed

Add takeTo to Traversable #2963

scabug opened this issue Jan 25, 2010 · 9 comments

Comments

@scabug
Copy link

scabug commented Jan 25, 2010

In the past few months I have sometimes wanted a takeTo method, which would take all elements up to and including the first for which a predicate was true. Or, in other words, the following equivalency would be true:

collection.takeTo(p) == collection.takeWhile(!p).init

I haven't found easy way to simulate such method using the existing ones. I have either used sliding, with the inconvenients it presents, or simply went about my code in a completely different manner.

So I ask for this method to be added. The name I propose is based on the until/to distinction used for ranges.

@scabug
Copy link
Author

scabug commented Jan 25, 2010

Imported From: https://issues.scala-lang.org/browse/SI-2963?orig=1
Reporter: @dcsobral

@scabug
Copy link
Author

scabug commented Jan 27, 2010

@dcsobral said:
I hope this wasn't decided because of my misrepresentation of semantics? The equality I intended was

collection.takeTo(p).init == collection.takeWhile(!p)

Though I only now realize I mistyped it.

@scabug
Copy link
Author

scabug commented Jan 27, 2010

@paulp said:
Replying to [comment:3 dcsobral]:

I hope this wasn't decided because of my misrepresentation of semantics?

More likely because you're suggesting library changes at about the worst possible time. FWIW I want this function all the time as well, but please let us try to ship 2.8. That means live with the library the way it is short of glaring bugs. The faster we ship the faster I can open the greenhouse.

@scabug
Copy link
Author

scabug commented Jan 27, 2010

@SethTisue said:
takeWhile forms a trio with dropWhile and span. if takeTo is added at some time in the future, perhaps dropTo and spanTo should go in as well. not sure it makes sense to do just one without doing all three. (not sure on the names either)

I not infrequently find myself wanting this (takeTo/dropTo/spanTo — it's hard to remember which, since they're really the same underlying thing)

do the Haskell people have a name for this?

@scabug
Copy link
Author

scabug commented Jan 27, 2010

@dcsobral said:
I'm just putting in an enhancement request. Nobody should be concerned about an enhancement ticket languishing even for years, much less it being postponed for a few months while a release is finished.

So I hope even more that this wasn't closed just because people are focusing on 2.8.

@scabug
Copy link
Author

scabug commented Jan 27, 2010

@lindydonna said:
Sorry, I forgot to add the comment initially. The feeling of the team was that this is not a useful enough feature to warrant adding more methods to the library.

@scabug
Copy link
Author

scabug commented Jul 13, 2010

Steven Bethard (bethard) said:
For what it's worth, this is exactly the method I needed for grouping items in an iterable by looking for a sentinel value:

http://stackoverflow.com/questions/3231160/grouping-items-in-an-iterable-by-looking-for-a-sentinel-value-in-scala

This isn't the first time I've needed this either. I actually think takeTo is much more useful than takeWhile, since you can always get the takeWhile result from the takeTo result, but not vice versa. That said, I don't know how to address your concern that there are too many methods in the library.

@scabug scabug closed this as completed May 18, 2011
@scabug
Copy link
Author

scabug commented Dec 30, 2011

@dcsobral said (edited on Dec 30, 2011 3:10:46 PM UTC):
I realized I never gave a correct definition, so here it is.

def takeTo(p: A => Boolean): Repr = {
  val (before, after) = this span p
  before :+ after.head
}

def dropTo(p: A => Boolean): Repr = this dropWhile p tail

def spanTo(p: A => Boolean): Repr = {
  val (before, after) = this span p
  (before :+ after.head, after.tail)
}

@SethTisue
Copy link
Member

a newer, open ticket on this is https://github.com/scala/scala-dev/issues/417

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants