Logo

Programming-Idioms

History of Idiom 189 > diff from v9 to v10

Edit summary for version 10 by programming-idioms.org:
New Go implementation by user [programming-idioms.org]

Version 9

2019-09-27, 14:03:48

Version 10

2019-09-27, 14:11:21

Idiom #189 Filter and transform list

Produce a new list y containing the result of function T applied to all elements e of list x that match the predicate P.

Idiom #189 Filter and transform list

Produce a new list y containing the result of function T applied to all elements e of list x that match the predicate P.

Extra Keywords
map apply
Extra Keywords
map apply
Code
var y []Result
for _, e := range x {
	if P(e) {
		y = append(y, T(e))
	}
}
Comments bubble
No functional style: just a regular loop.

y is not fully allocated upfront because the number of matching elements is not known yet.
Demo URL
https://play.golang.org/p/3k8l3j6uM1K