boostorg / hana

Your standard library for metaprogramming
http://boostorg.github.io/hana
Boost Software License 1.0
1.7k stars 216 forks source link

pipe operator as in ranges TS #438

Open godefv opened 5 years ago

godefv commented 5 years ago

It would be nice to have the same syntax as the ranges TS.

For example, instead of

boost::hana::fold(
  boost::hana::transform(
    boost::hana::values(map)
  ,func)
,boost::hana::union_)

I would write

map|boost::hana::values
   |boost::hana::transform(func)
   |boost::hana::fold(boost::hana::union_)
ricejasonf commented 5 years ago

Checkout hana::tuple, hana::lazy, or hana::optional Here the operator| is typically used with hana::Monads

For point-free programming check out Boost.Hof

godefv commented 5 years ago

ok, I didn't know about that. It seems that the idea of the operator| for monads is indeed chaining functions, but it doesn't work to chain transforms, folds, etc, right ?

godefv commented 5 years ago

I have tried to used boost::hana::operator| like this :

map|boost::hana::values
   |[](auto const& x){return boost::hana::transform(x, func);}
   |[](auto const& x){return boost::hana::fold(x, boost::hana::union_);}

The compiler complains that transform requires a functor as x.

So, I have defined myself auto operator|(T&&, Invokable<T>&&). Without the Invokable constraint, it conflicts with boost::hana::operator|, but with it, it seems to work.

It would be nicer to be able to use boost::hana::transform(boost::hana::_, func). Is it intended that this syntax is not allowed ? EDIT : actually, I have not tried it again with my own operator, it might work...

ricejasonf commented 5 years ago

The placeholder _ is just an object that supports some operators. Boost.Hof is probably what you are looking for if you want high order functions.