[GiNaC-devel] Best way to iterate through an expression

Ricardo Buring ricardo.buring at gmail.com
Wed May 10 17:47:37 CEST 2017


Dear Jan,

Here's a trick I've used to avoid such code duplication:

if (!is_a<mul>(some_ex))
    some_ex = lst(some_ex);
for (const auto& m : some_ex) {
    // Do something with m
}

If some_ex is a product then you iterate over the factors, and
otherwise you iterate over the 1-element lst containing only some_ex.
Perhaps this fits your use case as well.

Best wishes,
Ricardo


On Mon, May 8, 2017 at 8:06 PM, Jan Rheinländer <jrheinlaender at gmx.de> wrote:
> Hi,
>
> my code has a lot of statements of this kind:
>
> if (is_a<mul>(some_ex)) {
>     for (const auto& m : some_ex) {
>       // Do something with m
>     }
>   } else  {
>     // Do something with some_ex
>   }
>
> Is there a more elegant way of iterating through an expression that
> avoids having to write the "Do something" part twice?
>
> Thanks,
>
> Jan
>
> _______________________________________________
> GiNaC-devel mailing list
> GiNaC-devel at ginac.de
> https://www.cebix.net/mailman/listinfo/ginac-devel


More information about the GiNaC-devel mailing list