开发者

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

开发者 https://www.devze.com 2023-01-25 04:15 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消