Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Lua

Idiom #57 Filter list

Create the list y containing the items from the list x that satisfy the predicate p. Respect the original ordering. Don't modify x in-place.

y = {}
for _, v in ipairs(x) do
	if p(v) then y[#y+1] = v end
end
local y={}
for _,v in ipairs(x) do
 if p(v)==true then 
  table.insert(y,v)
 end
end
for Item of X loop
   if P (Item) then
      Y.Append (Item);
   end if;
end loop;

New implementation...