Boost logo

Boost Users :

Subject: Re: [Boost-users] chaining functions input output
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2012-09-19 16:29:41


> >> i have these 2 functions provided to me and I wonder whether they can
> >> somehow be chained without using intermediary memory?
> >>
> >> http://codepad.org/tTwkUQhp
> >
> > No.
> > You have to supply a range [begin..end) for fun2(), so this range
> > should be prepared first.
> >
> someone mentioned boost.coroutine could help in this case?

I'm the one who mentioned that, and yes, it is possible :)

Basically, one can use coroutines to implement a generic outputs_of()
function that takes a unary function whose argument is an output
iterator, and returns a range representing all the values passed to
this output iterator, without storing them all.

You would call it like this:

fun2(outputs_of(bind(fun1, input, _1)), output)

I actually wrote such a function, but using an earlier version of
the coroutine library (back when the coroutine functionality was
still in Boost.Context). I am planning to update my implementation
after the interface of Boost.Coroutine is stabilized (it is
currently under review on the dev list). I am happy to post it
once I've done that, but in the meantime here's the general idea:

- outputs_of(f) creates a coroutine c whose body calls f with an
  output iterator which yields the outputted value
- begin() of the resulting range starts the coroutine and saves
  the first yielded value
- dereference() of the begin iterator returns the saved value
- increment() of the begin iterator re-enters the coroutine and
  saves the next yielded value
- when the coroutine is finished a flag is set to that the begin
  and end iterators return true

Hope that helps!

Regards,
Nate
                                               


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net