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!
Related posts:
- Organising GMail’s Inbox and Weirdness with Starring Items If you’re like me, you’ve already thrown out the system...
- Hiding Items From Windows Update having freshly reinstalled Vista Ultimate on my work laptop, Windows...
- Skype Listens on Port 80 By default, skype grabs a hold on incoming requests on...
- References are not addresses Just read this excellent post by Eric Lippert about why...
- Instant Twitter Bookmark Tonight’s been a busy night for silly coding This first...
Doh, I’ve never thought of that either. Nice one!
Just found your blog via the ozalt group / site. Always good to find new peeps.
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