Logo

Programming-Idioms

History of Idiom 57 > diff from v28 to v29

Edit summary for version 29 by :
[Go]Fixed alloc len/cap, +new demo URL

Version 28

2015-12-08, 21:32:53

Version 29

2015-12-08, 21:34:21

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 := make([]T, 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/DmmgjHXb-F
Demo URL
http://play.golang.org/p/QTg6RZFtp7