www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D2: std.algorithm.find, but get part before what you were searching for

reply Jonathan M Davis <jmdavisProg gmail.com> writes:
std.algorithm.find() returns the rest of the range starting at what you were 
searching for (or an empty range if it wasn't in the given range). Is there  
a function in phobos which does a find but returns everything _before_ what 
you're searching for?

I can't find one that will do that, and I don't see an obvious way of 
combining functions to get it. Naturally, I'll roll my own function for it 
if I have to, but if there's already a way to do it in phobos semi-cleanly, 
I'd probably prefer to do that. So, I'm enquiring as to whether anyone here 
knows of such a function or combination of functions. Thanks.

- Jonathan M Davis
Mar 04 2010
parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
Jonathan M Davis wrote:
 std.algorithm.find() returns the rest of the range starting at what you were 
 searching for (or an empty range if it wasn't in the given range). Is there  
 a function in phobos which does a find but returns everything _before_ what 
 you're searching for?
 
 I can't find one that will do that, and I don't see an obvious way of 
 combining functions to get it. Naturally, I'll roll my own function for it 
 if I have to, but if there's already a way to do it in phobos semi-cleanly, 
 I'd probably prefer to do that. So, I'm enquiring as to whether anyone here 
 knows of such a function or combination of functions. Thanks.
 
 - Jonathan M Davis
Would std.algorithm.until() be what you're looking for? http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#until -Lars
Mar 04 2010
parent reply Jonathan M Davis <jmdavisProg gmail.com> writes:
Lars T. Kyllingstad wrote:

 Jonathan M Davis wrote:
 std.algorithm.find() returns the rest of the range starting at what you
 were searching for (or an empty range if it wasn't in the given range).
 Is there a function in phobos which does a find but returns everything
 _before_ what you're searching for?
 
 I can't find one that will do that, and I don't see an obvious way of
 combining functions to get it. Naturally, I'll roll my own function for
 it if I have to, but if there's already a way to do it in phobos
 semi-cleanly, I'd probably prefer to do that. So, I'm enquiring as to
 whether anyone here knows of such a function or combination of functions.
 Thanks.
 
 - Jonathan M Davis
Would std.algorithm.until() be what you're looking for? http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#until -Lars
Hmm. I'd thought that I'd looked at that one and determined that it didn't do what I was looking for, but on another inspection of it, it does look like until() will do the job. It has the potential downside that unlike find, it cannot take multiple ranges to be found, but in my case (and probably most cases), that's not an issue. Thanks. Maybe this is what I get for coding late at night... On a side note, it would be great if we could figure out a way to make the docs more user-friendly - especially in std.algorithm. The functions are great, and they're really versatile, but that seems to come at the cost of incredibly nasty signatures. It can make it hard to find the function itself among all of its parameters and its return type (especially the return type). Right now, I'd expect it to scare a fair number of potential users away. What the best solution would be, I don't know (perhaps making it so that it lists information on the function without its signature and then lists the signature at the end, or maybe we could find some way to give an abstracted signature which gave the basic idea without all of the details), but right now std.algorthm is definitely scary-looking. It's got great stuff, but the docs could definitely use some improvement. - Jonathan M Davis
Mar 04 2010
next sibling parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
Jonathan M Davis wrote:
 Lars T. Kyllingstad wrote:
 
 Jonathan M Davis wrote:
 std.algorithm.find() returns the rest of the range starting at what you
 were searching for (or an empty range if it wasn't in the given range).
 Is there a function in phobos which does a find but returns everything
 _before_ what you're searching for?

 I can't find one that will do that, and I don't see an obvious way of
 combining functions to get it. Naturally, I'll roll my own function for
 it if I have to, but if there's already a way to do it in phobos
 semi-cleanly, I'd probably prefer to do that. So, I'm enquiring as to
 whether anyone here knows of such a function or combination of functions.
 Thanks.

 - Jonathan M Davis
Would std.algorithm.until() be what you're looking for? http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#until -Lars
Hmm. I'd thought that I'd looked at that one and determined that it didn't do what I was looking for, but on another inspection of it, it does look like until() will do the job. It has the potential downside that unlike find, it cannot take multiple ranges to be found, but in my case (and probably most cases), that's not an issue. Thanks. Maybe this is what I get for coding late at night...
I think the problem is the description: "Lazily iterates range until value sentinel is found, at which point it stops." It doesn't say anything about returning the range up to that point. You have to look at the example to see what it does.
 On a side note, it would be great if we could figure out a way to make the 
 docs more user-friendly - especially in std.algorithm. The functions are 
 great, and they're really versatile, but that seems to come at the cost of 
 incredibly nasty signatures. It can make it hard to find the function itself 
 among all of its parameters and its return type (especially the return 
 type). Right now, I'd expect it to scare a fair number of potential users 
 away. What the best solution would be, I don't know (perhaps making it so 
 that it lists information on the function without its signature and then 
 lists the signature at the end, or maybe we could find some way to give an 
 abstracted signature which gave the basic idea without all of the details), 
 but right now std.algorthm is definitely scary-looking. It's got great 
 stuff, but the docs could definitely use some improvement.
I agree. There are several things which could be done: 1. Alphabetical sorting of the list of functions at the top of the page. This has already been implemented by Adam D. Ruppe and will be published with the next DMD release, AFAIK. 2. Better separation of elements that share a doc comment. until() is a great example: You have to look more than twice at the signature to see that it is actually three signatures -- one struct and two functions. 3. It's sometimes unclear which symbols are members of a class/struct and which are module-level symbols. Tango's API documentation is better in this regard, since it has the symbol tree on the left-hand side of the page. -Lars
Mar 04 2010
parent "Nick Sabalausky" <a a.a> writes:
"Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> wrote in message 
news:hmnu5l$31j6$1 digitalmars.com...
   3. It's sometimes unclear which symbols are members of a class/struct 
 and which are module-level symbols.  Tango's API documentation is better 
 in this regard, since it has the symbol tree on the left-hand side of the 
 page.
For me, Tango's API docs don't have that tree on the left-side. That's because it only shows up with JS on, and I have JS disabled for the tango site because it makes the API docs slow enough to be unusable.
Mar 06 2010
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 04 Mar 2010 03:55:40 -0500, Jonathan M Davis  
<jmdavisProg gmail.com> wrote:

 On a side note, it would be great if we could figure out a way to make  
 the
 docs more user-friendly - especially in std.algorithm. The functions are
 great, and they're really versatile, but that seems to come at the cost  
 of
 incredibly nasty signatures. It can make it hard to find the function  
 itself
 among all of its parameters and its return type (especially the return
 type). Right now, I'd expect it to scare a fair number of potential users
 away. What the best solution would be, I don't know (perhaps making it so
 that it lists information on the function without its signature and then
 lists the signature at the end, or maybe we could find some way to give  
 an
 abstracted signature which gave the basic idea without all of the  
 details),
 but right now std.algorthm is definitely scary-looking. It's got great
 stuff, but the docs could definitely use some improvement.
It is an issue with DDoc. I think the signatures are not that nasty if you look at the actual code. I think DDoc expands all aliases so you get the full template-expanded signature. -Steve
Mar 04 2010
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Thu, Mar 4, 2010 at 13:12, Steven Schveighoffer <schveiguy yahoo.com>wrote:

 It is an issue with DDoc.  I think the signatures are not that nasty if you
 look at the actual code.  I think DDoc expands all aliases so you get the
 full template-expanded signature.
The problem also is that when you use 'auto' to simplify your code, DDoc doesn't process the associated documentation comment! Gee, I wonder if that's somewhere in bugzilla? Ah, there: http://d.puremagic.com/issues/show_bug.cgi?id=2581 Else I'd use auto much more often. As it is, the only solution I found to avoid monstrous many-lines templated types is something suggested here: have the type produced by a wrapper template. It duplicates the work for the library writer, but it's easier on the eye for the user. /** Lazily returns all subranges of a range, beginning with an empty range. */ SubRanges!R subranges(R)(R range) {...} Philippe
Mar 04 2010
prev sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Jonathan M Davis wrote:

 Hmm. I'd thought that I'd looked at that one and determined that it didn't 
 do what I was looking for, but on another inspection of it, it does look 
 like until() will do the job. It has the potential downside that unlike 
 find, it cannot take multiple ranges to be found, but in my case (and 
 probably most cases), that's not an issue. Thanks. Maybe this is what I get 
 for coding late at night...
You should be able to use std.range.chain to combine your ranges.
Mar 04 2010
parent Jonathan M Davis <jmdavisProg gmail.com> writes:
Jesse Phillips wrote:

 Jonathan M Davis wrote:
 
 Hmm. I'd thought that I'd looked at that one and determined that it
 didn't do what I was looking for, but on another inspection of it, it
 does look like until() will do the job. It has the potential downside
 that unlike find, it cannot take multiple ranges to be found, but in my
 case (and probably most cases), that's not an issue. Thanks. Maybe this
 is what I get for coding late at night...
You should be able to use std.range.chain to combine your ranges.
I don't think that that would work. As I understand it, with find, when you give it call it with more than two values e.g. find(haystack, needle1, needle2, needle3) instead of find(haystack, needle) it looks for all of those values and returns when it finds one of them. All combining ranges would do for until would make a longer thing to find. In essence, you'd be doing find(haystack, needle1 ~ needle2 ~ needle3) only with until. Finding all of the values concatenated together is not the same thing as looking for the first occurrence of one of those values. - Jonathan M Davis
Mar 04 2010