Logo

Programming-Idioms

History of Idiom 57 > diff from v27 to v28

Edit summary for version 28 by :
[Go]Better have only 1 allocation

Version 27

2015-11-30, 12:37:29

Version 28

2015-12-08, 21:32:53

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
y := []T{}
for _, v := range x{
	if p(v){
		y = append(y, v)
	}
}
Code
y := make([]T, 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/DmmgjHXb-F
Demo URL
http://play.golang.org/p/DmmgjHXb-F