Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Misc Library
    • Labels:
      None
    • Environment:

      collection

      Description

      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.

        Activity

        Hide
        Daniel Sobral added a comment -

        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.

        Show
        Daniel Sobral added a comment - 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.
        Hide
        Paul Phillips added a comment -

        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.

        Show
        Paul Phillips added a comment - 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.
        Hide
        Seth Tisue added a comment -

        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?

        Show
        Seth Tisue added a comment - 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?
        Hide
        Daniel Sobral added a comment -

        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.

        Show
        Daniel Sobral added a comment - 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.
        Hide
        Donna Mithra Malayeri added a comment -

        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.

        Show
        Donna Mithra Malayeri added a comment - 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.
        Hide
        bethard added a comment -

        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.

        Show
        bethard added a comment - 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.
        Hide
        Daniel Sobral added a comment - - edited

        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)
        }
        
        Show
        Daniel Sobral added a comment - - edited 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) }

          People

          • Assignee:
            Unassigned
            Reporter:
            Daniel Sobral
            TracCC:
            Bryan, Paul Phillips, Seth Tisue
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development