Boost logo

Boost :

Subject: Re: [boost] Interest in a list comprehension library?
From: Brent Spillner (spillner_at_[hidden])
Date: 2010-12-19 23:01:16


Dave Abrahams wrote:

>I'm usually interested in comprehensions like (in Python):
>
>   [ x, foo(x) for x in some_other_sequence ]
>
>and your consistent use of range() above obscures whether "for x in
>some_other_sequence" can be modeled in general.  Can I substitute any
>sequence for range(...) in the above examples?

Yes, sorry for not making that clear. Continuing the prime number
example, you can generate Mersenne primes with

  std::vector<int> mersenne_primes = _((1 << p) - 1)(7)[p <<= primes,
apply(is_prime, (1 << p) - 1)];

This illustrates several points that I neglected before-- use of
apply() to invoke a function object with arguments, specification of a
finite limit (7) on the number of values to be returned (assigning to
a container via <<= would also implicitly limit it to the size of that
container), and the fact that I haven't yet allowed any assignment or
side-effect (increment/decrement) operators within the comprehension
body. My initial instinct was that that wouldn't be very FP-ish, but
now I'm thinking it would be handier to allow things like 'm = (1 <<
p) - 1' or 'count++' within the condition list.

I uploaded a new version to the Vault that includes the Mersenne
generator example and a few bugfixes.

>Anyway, very promising.  Keep up the great work!

Thanks, I appreciate the encouragement!

Brent


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk