Dean Michael Berris | 22 Sep 08:02

[fusion] Short-circuited Exception-based View Iteration

Hi Everyone,

I'm not sure if the title makes sense, but I have a problem which can
be solved with a handful of preprocessor macros, but would like to see
solved with Fusion.

Let's say I have a vector of values:

fusion::vector<int, int, int, int> ints(0, 1, 2, 3);

I then have a function such as:

struct function {
  void operator()(int value) {
    if (value < 2)
      throw runtime_error("less than 3");
    cout << value << endl;
  };
};

Would there be a way for me to iterate through every element of 'ints'
and apply the function object instance to each element, and stop the
iteration only when function does not throw?

I know using exceptions for control flow is pretty frowned upon, and I
can certainly use it the other way around -- to short-circuit the
iteration only when it throws.

If you're interested in the use case for this particular question, I'm
looking for a way to make the dispatcher library I'm working on
(Continue reading)

Tobias Schwinger | 22 Sep 14:26
Favicon

Re: [fusion] Short-circuited Exception-based View Iteration

Dean Michael Berris wrote:
> Hi Everyone,
> 
> I'm not sure if the title makes sense, but I have a problem which can
> be solved with a handful of preprocessor macros, but would like to see
> solved with Fusion.
> 
> Let's say I have a vector of values:
> 
> fusion::vector<int, int, int, int> ints(0, 1, 2, 3);
> 
> I then have a function such as:
> 
> struct function {
>   void operator()(int value) {
>     if (value < 2)
>       throw runtime_error("less than 3");
>     cout << value << endl;
>   };
> };
> 
> Would there be a way for me to iterate through every element of 'ints'
> and apply the function object instance to each element, and stop the
> iteration only when function does not throw?

No.

> I know using exceptions for control flow is pretty frowned upon, 

...and pretty inefficient, unless to terminate a deep recursion.
(Continue reading)

Joel de Guzman | 22 Sep 22:54

Re: [fusion] Short-circuited Exception-based View Iteration

Tobias Schwinger wrote:

> Reading the rest of your post, it seems you're basically looking for 
> algorithms like these:
> 
>     visit_if(sequence_reference,runtime_predicate,action)
>     visit_where_first(sequence_reference,runtime_predicate,action)

Seems those are specializations of:

     any(seq, f)

??

Description

For a sequence seq and unary function object f, any returns true if f returns 
true for at least one element of seq.

Regards,
--

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
(Continue reading)

Tobias Schwinger | 22 Sep 23:17
Favicon

Re: [fusion] Short-circuited Exception-based View Iteration

Joel de Guzman wrote:
> Tobias Schwinger wrote:
> 
>> Reading the rest of your post, it seems you're basically looking for 
>> algorithms like these:
>>
>>     visit_if(sequence_reference,runtime_predicate,action)
>>     visit_where_first(sequence_reference,runtime_predicate,action)
> 
> Seems those are specializations of:
> 
>      any(seq, f)
> 
> ??
> 
> Description
> 
> For a sequence seq and unary function object f, any returns true if f returns 
> true for at least one element of seq.

Yep, that is for the latter (based on the reasonable assumption that any 
checks by iterating the sequence in forward direction :-)).

The former would be 'count_if'.

Regards,
Tobias

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
(Continue reading)

Dean Michael Berris | 23 Sep 15:39

Re: [fusion] Short-circuited Exception-based View Iteration

Hi Joel!

On 9/22/07, Joel de Guzman <joel <at> boost-consulting.com> wrote:
> Tobias Schwinger wrote:
>
> > Reading the rest of your post, it seems you're basically looking for
> > algorithms like these:
> >
> >     visit_if(sequence_reference,runtime_predicate,action)
> >     visit_where_first(sequence_reference,runtime_predicate,action)
>
> Seems those are specializations of:
>
>      any(seq, f)
>
> ??
>
> Description
>
> For a sequence seq and unary function object f, any returns true if f returns
> true for at least one element of seq.
>

Almost... But 'any' will just return a boolean, but not which element
in seq (or a view of which elements in seq) where f returns true.
Perhaps 'filter' is what I'm looking for, but I'm just interested in
the first one where a predicate holds true, so even a find_if(...)
might suit my needs.

I'll explore a bit more.
(Continue reading)

Joel de Guzman | 23 Sep 16:40

Re: [fusion] Short-circuited Exception-based View Iteration

Dean Michael Berris wrote:

>>      any(seq, f)
>>
>> ??
>>
>> Description
>>
>> For a sequence seq and unary function object f, any returns true if f returns
>> true for at least one element of seq.
>>
> 
> Almost... But 'any' will just return a boolean, but not which element
> in seq (or a view of which elements in seq) where f returns true.

Do it in f where you know both.

> Perhaps 'filter' is what I'm looking for, but I'm just interested in
> the first one where a predicate holds true, so even a find_if(...)
> might suit my needs.

No. There's no way to do that. You can't have a runtime predicate
determine a compile time return type.

> I'll explore a bit more.
> 
> BTW, i haven't found a `find_first_if(...)` implementation (like what
> the STL has) which works with a homogeneous tuple -- somehow that
> container might be a special case that deserves attention (?) -- which
> returns a single value? Thoughts on this?
(Continue reading)

Dean Michael Berris | 24 Sep 06:39

Re: [fusion] Short-circuited Exception-based View Iteration

On 9/23/07, Joel de Guzman <joel <at> boost-consulting.com> wrote:
> Dean Michael Berris wrote:
> >
> > Almost... But 'any' will just return a boolean, but not which element
> > in seq (or a view of which elements in seq) where f returns true.
>
> Do it in f where you know both.
>

This may make f a bit bloated for my taste, but this will work. Thanks. :)

> > Perhaps 'filter' is what I'm looking for, but I'm just interested in
> > the first one where a predicate holds true, so even a find_if(...)
> > might suit my needs.
>
> No. There's no way to do that. You can't have a runtime predicate
> determine a compile time return type.
>

There might be special cases where this might happen though... Let me
illustrate below what I mean...

> > I'll explore a bit more.
> >
> > BTW, i haven't found a `find_first_if(...)` implementation (like what
> > the STL has) which works with a homogeneous tuple -- somehow that
> > container might be a special case that deserves attention (?) -- which
> > returns a single value? Thoughts on this?
> >
> > Maybe this will solve at least my problem... :D
(Continue reading)

shunsuke | 24 Sep 10:00

Re: [fusion] Short-circuited Exception-based View Iteration

Dean Michael Berris wrote:
> struct equals_2 {
>   template <typename T>
>   T operator() (T element) const {
>     if (element == 2) return element;
>   };
> };
> 
> typedef fusion::tuple<int, int, int> point_type;
> point_type point(1, 2, 3);
> assert(fusion::is_homogenous<point_type>::value == true);
> assert(fusion::find_first_if<equals_2>(point) == 2); // only works if
> 'point' is a homogeneous container

FWIW, a few weeks ago, I had a radical idea
about an iterator which runs through a tuple/fusion.

rectangle r; triangle t; circle c;
boost::tuple<rectangle*, triangle*, circle*> tup(&r, &t, &c);
BOOST_FOREACH (shape *s, oven::hetero<shape *>(tup)) {
     s->draw();
}

If interested, here is the implementation: http://tinyurl.com/2gojyw

Regards,

--

-- 
Shunsuke Sogame

(Continue reading)

Joel de Guzman | 24 Sep 10:20

Re: [fusion] Short-circuited Exception-based View Iteration

shunsuke wrote:
> Dean Michael Berris wrote:
>> struct equals_2 {
>>   template <typename T>
>>   T operator() (T element) const {
>>     if (element == 2) return element;
>>   };
>> };
>>
>> typedef fusion::tuple<int, int, int> point_type;
>> point_type point(1, 2, 3);
>> assert(fusion::is_homogenous<point_type>::value == true);
>> assert(fusion::find_first_if<equals_2>(point) == 2); // only works if
>> 'point' is a homogeneous container
> 
> FWIW, a few weeks ago, I had a radical idea
> about an iterator which runs through a tuple/fusion.
> 
> rectangle r; triangle t; circle c;
> boost::tuple<rectangle*, triangle*, circle*> tup(&r, &t, &c);
> BOOST_FOREACH (shape *s, oven::hetero<shape *>(tup)) {
>      s->draw();
> }
> 
> If interested, here is the implementation: http://tinyurl.com/2gojyw

I'm interested! This ought to be part of fusion :-)

Regards,
--

-- 
(Continue reading)

Joel de Guzman | 24 Sep 10:14

Re: [fusion] Short-circuited Exception-based View Iteration

Dean Michael Berris wrote:
> On 9/23/07, Joel de Guzman <joel <at> boost-consulting.com> wrote:
>> Dean Michael Berris wrote:
>>> Almost... But 'any' will just return a boolean, but not which element
>>> in seq (or a view of which elements in seq) where f returns true.
>> Do it in f where you know both.
>>
> 
> This may make f a bit bloated for my taste, but this will work. Thanks. :)

Perhaps you misunderstood what I mean. You can use function composition
to factor out the function in as high a granularity as you desire.
You do not have to stuff f with all the functionalities, you *compose*
f from basic primitives. That's the basic essence of functional programming.

Regards,
--

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Joel de Guzman | 24 Sep 10:17

Re: [fusion] Short-circuited Exception-based View Iteration

Dean Michael Berris wrote:

>> The world fusion (and for that matter, mpl) lives in has some peculiar
>> constraints. As I noted above, one is that you cannot determine a type
>> from a runtime predicate. You can, however, do it inside-out. IOTW,
>> let the predicate itself get the info that you need. If you really
>> need to return a type, somehow, you can, for example, use a pointer
>> to a base class, hold the value in a boost::any, etc. In other words
>> use type erasure. If I were you, I'd use the visitor, f, itself to
>> do the action as soon as the correct type is obtained. No type
>> erasure magic.
>>
> 
> This makes sense, though there might be a possibility where a
> homogeneous tuple would be able to return just a single value:
> 
> struct equals_2 {
>   template <typename T>
>   T operator() (T element) const {
>     if (element == 2) return element;
>   };
> };
> 
> typedef fusion::tuple<int, int, int> point_type;
> point_type point(1, 2, 3);
> assert(fusion::is_homogenous<point_type>::value == true);
> assert(fusion::find_first_if<equals_2>(point) == 2); // only works if
> 'point' is a homogeneous container

If that was the case, I'd use boost::array which is a nice data structure
(Continue reading)

Re: [Spirit-devel] [fusion] Short-circuited Exception-based ViewIteration

> -----Original Message-----
> From: spirit-devel-bounces <at> lists.sourceforge.net [mailto:spirit-devel-
> bounces <at> lists.sourceforge.net] On Behalf Of Joel de Guzman
> Sent: Monday, September 24, 2007 1:18 AM
> To: spirit-devel <at> lists.sourceforge.net
> Cc: boost <at> lists.boost.org
> Subject: Re: [Spirit-devel] [fusion] Short-circuited Exception-based
> ViewIteration
> 
> Dean Michael Berris wrote:
> >
> > This makes sense, though there might be a possibility where a
> > homogeneous tuple would be able to return just a single value:
> >
> > struct equals_2 {
> >   template <typename T>
> >   T operator() (T element) const {
> >     if (element == 2) return element;
> >   };
> > };
> >
> > typedef fusion::tuple<int, int, int> point_type;
> > point_type point(1, 2, 3);
> > assert(fusion::is_homogenous<point_type>::value == true);
> > assert(fusion::find_first_if<equals_2>(point) == 2); // only works
if
> > 'point' is a homogeneous container
> 
> If that was the case, I'd use boost::array which is a nice data
structure
(Continue reading)

Dean Michael Berris | 23 Sep 15:29

Re: [Spirit-devel] [fusion] Short-circuited Exception-based View Iteration

Hi Tobias!

On 9/22/07, Tobias Schwinger <tschwinger <at> isonews2.com> wrote:
> Dean Michael Berris wrote:
> >
> > Would there be a way for me to iterate through every element of 'ints'
> > and apply the function object instance to each element, and stop the
> > iteration only when function does not throw?
>
> No.
>

I thought so. That confirms my suspicion that there isn't an obvious
way of doing it. :-)

> > I know using exceptions for control flow is pretty frowned upon,
>
> ...and pretty inefficient, unless to terminate a deep recursion.
>
> But to answer you're question, you can of course wrap another function
> around yours that throws if it doesn't catch anything :-).
>

This makes sense, but a little too convoluted for my taste. It's one
of the options, and I don't know if there's even an alternative
that'll be easy to use aside from the above suggestion. Thanks for the
idea. ;)

> Reading the rest of your post, it seems you're basically looking for
> algorithms like these:
(Continue reading)


Gmane