Boost logo

Boost :

Subject: Re: [boost] [mpl] multiset
From: Eric Niebler (eniebler_at_[hidden])
Date: 2015-03-12 19:23:28


On 3/12/2015 3:55 PM, Eric Niebler wrote:
> I'd very much like to add support for recursive lambdas. I can imagine
> the factorial example looking like this:
>
> using factorial_ = lambda_rec<_a,
> lazy::if_c<lazy::greater<_a, meta::size_t<0>>,
> lazy::multiplies<N, lazy::apply<_self, lazy::dec<_a>>> ,
> meta::size_t<1>>>;
> template<std::size_t N>
> using factorial = apply<factorial_, meta::size_t<N>>;

Well, that was easy:

template<typename ...Ts>
struct lambda_rec
{
    template<typename...Us>
    using apply = meta::apply<
        let<var<_self, lambda_rec>, lambda<Ts...>>,
        Us...>;
};

using factorial_ = lambda_rec<_a,
    lazy::if_<lazy::greater<_a, meta::size_t<0>>,
              lazy::multiplies<_a, lazy::apply<_self, lazy::dec<_a>>>,
              meta::size_t<1>>>;
template<std::size_t N>
using factorial = apply<factorial_, meta::size_t<N>>;

static_assert(factorial<0>::value == 1, "");
static_assert(factorial<1>::value == 1, "");
static_assert(factorial<2>::value == 2, "");
static_assert(factorial<3>::value == 6, "");
static_assert(factorial<4>::value == 24, "");

Metafunctions? We don't need no stinkin' metafunctions! :-)

-- 
Eric Niebler
Boost.org
http://www.boost.org

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