Java
import org.apache.commons.collections.*; CollectionUtils.filter(list, new Predicate() { public boolean evaluate(Object o) { return !((String)o).isEmpty(); } });
PHP
$list = array_filter($list, function($s) {return !empty($s);});
Python
l = [x for x in l if x]
JavaScript
l = _.filter(l, function(s){return s.length;});
憑著直覺寫的,如果不能動的話再說 XD
這種寫法通常好讀又簡單,不過有些語言要寫的囉嗦一點…
One response to “Filter is good”
因為這篇,我知道該怎麼用Java寫 filter 🙂