Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #189 Filter and transform list

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

var y []Result
for _, e := range x {
	if P(e) {
		y = append(y, T(e))
	}
}

No functional style: just a regular loop.

y is not fully allocated upfront because the number of matching elements is not known yet.
(def y 
  (eduction (filter P)
            (map T))
            x)

New implementation...