wk8 / go-ordered-map

Optimal implementation of ordered maps for Golang - ie maps that remember the order in which keys were inserted.
Apache License 2.0
534 stars 40 forks source link

Add "Filter" function #47

Open WilliamFrei opened 1 month ago

WilliamFrei commented 1 month ago

As noted in an issue, removing all elements from an OrderedMap except those that fulfill some criteria (known as "filtering" in functional contexts) is not straight-forward and contains a pitfall.

I have added a Filter function that is passed a predicate function and avoids this pitfall. The predicate function should return true if the key&value fit the criteria and should be kept and false otherwise, as may be familiar from other programming languages (e.g. python: filter)

Additionally I have added an example of this to the README. The new iteration syntax is now also showcased there.

WilliamFrei commented 1 month ago

closes #38