@result_list = grep { test($_) } @unfiltered_list in other languages?
One of the features I love in Perl is the LISP-inspired (?) abili开发者_如何学Pythonty 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.
In python there is the filter function:
result_list = filter(test,unfiltered_list)
In C++0x you can do this using std::copy_if
with a back_inserter
iterator. Lambda functions make this even easier too.
精彩评论