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

I use C++, and I have a variety of utility functions defined. To do his task, first I already would have the following defined:

  template <class Pred, class Sequence>
  Sequence filter(Pred p, const Sequence& in)
  {
    Sequence out; 
    for (typename Sequence::const_iterator i = in.begin(); i != in.end(); ++i) {
      if (p(*i)) {
        out.insert(out.end(), *i);
      }
    }
    return out;
  }
And then I would do:

  bool is_div3(const int n)
  {
    return n % 3;
  }

  // in another scope

  result = filter(is_div3, seq);
It's not how most people use C++, but my recent work has involved manipulating parse trees. I find the most natural way to do this is recursively using functional techniques, so I've built up a small set of functionaly inspired utility functions (https://github.com/scotts/cellgen/blob/master/src/utility.h). C++0x will let me use an anonymous function instead of having to define a function (or function object) for my predicate.


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

Search: