Don’t Overcomplicate It – The Simple Solution Is Always The Best

I could slap myself for not thinking of this sooner.

I have a list. I need to filter said list destructively (ie: modify the provided reference and not return a new list as a function result), and remove items which don’t meet criteria X.
My first dig at the code uses a for-loop and adds items to a second collection when the sub-item meets the criteria. Need to manage the incoming list, a new list and make sure i’ve overwritten the list reference at the end. Messy and as i discovered, error-prone

or….use a for-loop, and count backwards from Length to 0 and delete as i go.

stupido!

2 comments

  1. In Smalltalk the method would look like…

    filter: aList basedOn: aCriteriaBlock

    ^ aList := aList reject: aCriteria

    … which for me is simpler and easier to understand than worrying what the most efficient manner of iterating through the list and rejecting certain items 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *