Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Minor point: Extensive use of GNU extensions, so not really C.

Major point: Very much easier to do this in C++ by overloading operators for list<>:

    L2 = L1 + L3; // L2 is L1 concat L3
    L2 = L1 < 3; // L2 is L1 elements less than 3
    L2 = L1 | even; // L2 is L1 elements passed through a filter for even numbers


There is a C++ library proposal to implement (lazy) range algorithms and overload the operator| for their composition:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n187...

it would allow to do things like

  std::copy( ptr_vec | std::reversed | std::indirected,
             std::ostream_iterator<int>( std::cout ) );
Boost.Range implements part of the proposal, a much more complete implementation is Oven (http://p-stade.sourceforge.net/oven/doc/html/index.html), which includes the pipe operator, but I think it is unmaintained.

The result can be very readable and robust, I've seen complex production code using extensively this syntax.


Now Oven is fortunately maintained by awesome hackers in Japan. The latest C++ should compile against the trunk.

Regards,


That is not an accurate translation. Using the two lists is not the same as using one list, then the other; it does the Cartesian closure. You've got other problems, too, such as the fact that won't compose anything like a monad will, but that will do for a start.

Monads are trivial to implement in other languages if you skip the way they use functions and thereby implement not-actually-a-Monad. I won't say C++ can't do a monad nicely but it certainly isn't going to be that easy.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: