www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3888] New: Version of find() which returns the range _before_ what was found

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3888

           Summary: Version of find() which returns the range _before_
                    what was found
           Product: D
           Version: 2.040
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: jmdavisProg gmail.com


--- Comment #0 from Jonathan M Davis <jmdavisProg gmail.com> 2010-03-07
04:47:12 PST ---
"abcdefg".find("de") will get you "defg" - everything from the part found
onwards. There is no corresponding function in phobos which would give you the
part _before_ what was found. e.g. "abcdefg".findBefore("de") would return
"abc" (though findBefore may not be the best name).

The until() function is the closest function to something like findBefore(),
but it really isn't the same thing. It will only compare single elements, and
doesn't take multiple needles, just one.

A version of until() that operated on the whole range rather than just the
first element when checking its predicate would definitely work, so maybe it
would be better to alter until() than come up with a new function - or perhaps
we should add a second version of until() that takes a range rather than a
Sentinel. Or we could add both a function which was the counterpart to find()
and a new version of until() which took a range instead of Sentinel - sometimes
it would be desirable to do exactly what find() does except return what was
before what was found, and sometimes it would be nice to have a more flexible
function that just checks the predicate (which probably wouldn't work with
multiple needles like find() does).

Another consideration is that there is no counterpart to until() which returns
what comes after (though a version of it which took a range rather than a
Sentinel would be better IMO). find() is more along the lines of a specialized
version of that.

So, what I'd like to see in phobos would be something along these lines:

1. A function (findBefore() ?) which has basically the same signature as find()
but returns the range before what's being found rather than the range starting
with what's being found.

2. A version of until() which takes a range rather than a Sentinel (though
maybe it would need a different name).

3. A function which has basically the same signature as until() but returns the
range starting with when the predicate is true rather than before.

4. A function which has basically the same signature as the new version of
until() which takes a range rather than a Sentinel but returns the range
starting with when the predicate is true rather than before.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 07 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3888


Shin Fujishiro <rsinfu gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rsinfu gmail.com


--- Comment #1 from Shin Fujishiro <rsinfu gmail.com> 2010-11-16 12:17:58 PST
---
I often want both parts before and after a needle at the same time.  So how
about bisect()?  It returns a tuple of the two subranges: (before, after).

For example:

  assert(bisect([ 1,2,3,4,5 ], 3) == tuple([ 1,2 ], [ 3,4,5 ]));

Another name might be appropriate seeing issue 4787 though.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 16 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3888



--- Comment #2 from Jonathan M Davis <jmdavisProg gmx.com> 2010-11-16 16:10:04
PST ---
I'd argue it would be nice to have both a function that returns before and one
that returns both before and after, but you could also argue that it would be
simpler just to have the one which returned both. Still, ideally, I'd think
that you'd want both - especially when trying to use findBefore() (or whatever
you'd call it) in the middle of an expression.

Regardless, however, a function like bisect() (or whatever it would be called)
probably should be added. I tend to be forced to use indexOf() in that sort of
situation, which doesn't tend to work as well - except perhaps when dealing
with arrays.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 16 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3888


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #3 from Jonathan M Davis <jmdavisProg gmx.com> 2011-02-19 03:03:13
PST ---
I think that findSplit, findSplitBefore, and findSplitAfter effectively fulfill
this enhancement request. In fact, they're thorough enough that they seem to
pretty much obsolete until from what I can tell. They're really quite nice.

In any case, I see no reason to leave this request open.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 19 2011