Logo

Programming-Idioms

History of Idiom 57 > diff from v33 to v34

Edit summary for version 34 by :
New Csharp implementation by user [javasucks]

Version 33

2016-02-17, 15:59:13

Version 34

2016-02-18, 16:58:00

Idiom #57 Filter list

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

Idiom #57 Filter list

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

Code
for Item of X loop
   if P (Item) then
      Y.Append (Item);
   end if;
end loop;
Imports
import std.algorithm;
Imports
import std.algorithm;
Code
auto y = [1, 2, 3, 4, 5].filter!(a => a%2==0);
Code
auto y = [1, 2, 3, 4, 5].filter!(a => a%2==0);
Comments bubble
Actual example.
y is a range containing only [2, 4]
Comments bubble
Actual example.
y is a range containing only [2, 4]
Code
y := make([]T, 0, len(x))
for _, v := range x{
	if p(v){
		y = append(y, v)
	}
}
Code
y := make([]T, 0, len(x))
for _, v := range x{
	if p(v){
		y = append(y, v)
	}
}
Comments bubble
For item type T.
Comments bubble
For item type T.
Demo URL
http://play.golang.org/p/QTg6RZFtp7
Demo URL
http://play.golang.org/p/QTg6RZFtp7