Logo

Programming-Idioms

History of Idiom 57 > diff from v37 to v38

Edit summary for version 38 by programming-idioms.org:
[Go] Comment += allocates memory

Version 37

2016-06-05, 15:31:49

Version 38

2016-06-05, 15:42:38

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, 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.
Note that this allocates memory for the new slice y.
Demo URL
http://play.golang.org/p/QTg6RZFtp7
Demo URL
http://play.golang.org/p/QTg6RZFtp7