@result_list = grep { test($_) } @unfiltered_list in other languages?

133 Views Asked by At

One of the features I love in Perl is the LISP-inspired (?) ability to filter content out of a list of things with the simple syntax

@result_list = grep { test($_) } @unfiltered_list;

where test function will be applied to all the items of @unfiltered_list to produce the @result_list.

Is that a feature that exists in other languages as well ? (PHP? Python?) Otherwise, how could I easily hint non-Perlers (students) about what I mean through such code ?

SOLUTION: filter in most languages, as seen on wikipedia. Thanks for the tip, dudes.

2

There are 2 best solutions below

0
On BEST ANSWER

In python there is the filter function:

result_list = filter(test,unfiltered_list)
3
On

In C++0x you can do this using std::copy_if with a back_inserter iterator. Lambda functions make this even easier too.