This week's LFE Friday was translated with permission from the Erlang Thursday series by Steven Proctor. This week's translator: Robert Virding.

Today’s LFE Friday is on lists:filter/2.

lists:filter/2 takes two arguments, a predicate function and a list to iterate over. The return value is a list of items for which the predicate function returns true for that item.

> (lists:filter (lambda (x) (=:= (rem x 2) 1)) '(1 2 3 4 5))
(1 3 5)
> (lists:filter #'erlang:is_atom/1 '(1 a 3 #(a b) World foo))
(a World foo)
> (lists:filter (lambda (x) (> x 0)) '(1 0 -3 foo -13 43))
(1 foo 43)
> (lists:filter (lambda (x) (> x 0)) ())
()
> (lists:filter (lambda (x) 'false) '(1 2 3 4 5))
()
> (lists:filter (lambda (x) 'true) '(1 2 3 4 5)) 
(1 2 3 4 5)

–Proctor, Robert



Author

Published

02 February 2015

Category

tutorials

Tags