www.digitalmars.com         C & C++   DMDScript  

D - opApply() question

reply "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
to support foreach() on a class, one implements the opApply() method with
the signature: int opApply (int delegate(inout Type) dg)

This is for iterating across all elements within the class (container). How
does one specify an opApply() that is capable of filtering which elements it
exposes?

For example: if I have a directory of names versus phone-numbers, how do I
set up foreach() and opApply() to return only those elements that match a
given surname?

- Kris
Apr 01 2004
parent reply "Matthew" <matthew stlsoft.org> writes:
"Kris" <someidiot earthlink.dot.dot.dot.net> wrote in message
news:c4in3o$2a00$1 digitaldaemon.com...
 to support foreach() on a class, one implements the opApply() method with
 the signature: int opApply (int delegate(inout Type) dg)

 This is for iterating across all elements within the class (container).
How
 does one specify an opApply() that is capable of filtering which elements
it
 exposes?

 For example: if I have a directory of names versus phone-numbers, how do I
 set up foreach() and opApply() to return only those elements that match a
 given surname?
You don't. Instead, you'd have a method - filter(char[] surname) - that returns something that is itself freachable, and which applies your filtering to the sequence provided by the directory's freachable range.
Apr 02 2004
parent reply "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
Cheers Matthew; that's just what I ended up doing -- adding little factory
methods for creating filtering freachables.

- Kris


"Matthew" <matthew stlsoft.org> wrote in message
news:c4j8ob$6df$1 digitaldaemon.com...
 "Kris" <someidiot earthlink.dot.dot.dot.net> wrote in message
 news:c4in3o$2a00$1 digitaldaemon.com...
 to support foreach() on a class, one implements the opApply() method
with
 the signature: int opApply (int delegate(inout Type) dg)

 This is for iterating across all elements within the class (container).
How
 does one specify an opApply() that is capable of filtering which
elements
 it
 exposes?

 For example: if I have a directory of names versus phone-numbers, how do
I
 set up foreach() and opApply() to return only those elements that match
a
 given surname?
You don't. Instead, you'd have a method - filter(char[] surname) - that returns something that is itself freachable, and which applies your filtering to the sequence provided by the directory's freachable range.
Apr 02 2004
parent reply "Matthew" <matthew stlsoft.org> writes:
Cool. I'd like to see them when you're comfortable to share. :)

"Kris" <someidiot earthlink.dot.dot.dot.net> wrote in message
news:c4jcgu$cpa$1 digitaldaemon.com...
 Cheers Matthew; that's just what I ended up doing -- adding little factory
 methods for creating filtering freachables.

 - Kris


 "Matthew" <matthew stlsoft.org> wrote in message
 news:c4j8ob$6df$1 digitaldaemon.com...
 "Kris" <someidiot earthlink.dot.dot.dot.net> wrote in message
 news:c4in3o$2a00$1 digitaldaemon.com...
 to support foreach() on a class, one implements the opApply() method
with
 the signature: int opApply (int delegate(inout Type) dg)

 This is for iterating across all elements within the class
(container).
 How
 does one specify an opApply() that is capable of filtering which
elements
 it
 exposes?

 For example: if I have a directory of names versus phone-numbers, how
do
 I
 set up foreach() and opApply() to return only those elements that
match
 a
 given surname?
You don't. Instead, you'd have a method - filter(char[] surname) - that returns something that is itself freachable, and which applies your filtering to the sequence provided by the directory's freachable range.
Apr 02 2004
parent "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
Certainly. This particular one was related to the traversal of HTTP headers
in the Dsc package.

- Kris

"Matthew" <matthew stlsoft.org> wrote in message
news:c4juoc$18se$1 digitaldaemon.com...
 Cool. I'd like to see them when you're comfortable to share. :)

 "Kris" <someidiot earthlink.dot.dot.dot.net> wrote in message
 news:c4jcgu$cpa$1 digitaldaemon.com...
 Cheers Matthew; that's just what I ended up doing -- adding little
factory
 methods for creating filtering freachables.

 - Kris


 "Matthew" <matthew stlsoft.org> wrote in message
 news:c4j8ob$6df$1 digitaldaemon.com...
 "Kris" <someidiot earthlink.dot.dot.dot.net> wrote in message
 news:c4in3o$2a00$1 digitaldaemon.com...
 to support foreach() on a class, one implements the opApply() method
with
 the signature: int opApply (int delegate(inout Type) dg)

 This is for iterating across all elements within the class
(container).
 How
 does one specify an opApply() that is capable of filtering which
elements
 it
 exposes?

 For example: if I have a directory of names versus phone-numbers,
how
 do
 I
 set up foreach() and opApply() to return only those elements that
match
 a
 given surname?
You don't. Instead, you'd have a method - filter(char[] surname) -
that
 returns something that is itself freachable, and which applies your
 filtering to the sequence provided by the directory's freachable
range.

Apr 02 2004