Home > IT & Software > Don’t Overcomplicate It – The Simple Solution Is Always The Best

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

January 22nd, 2009

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!

Bookmark this post:
  • DotNetKicks
  • DZone
  • TwitThis
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati

Related posts:

  1. Organising GMail’s Inbox and Weirdness with Starring Items If you’re like me, you’ve already thrown out the system...
  2. Hiding Items From Windows Update having freshly reinstalled Vista Ultimate on my work laptop, Windows...
  3. Skype Listens on Port 80 By default, skype grabs a hold on incoming requests on...
  4. References are not addresses Just read this excellent post by Eric Lippert about why...
  5. Instant Twitter Bookmark Tonight’s been a busy night for silly coding This first...

Xerxes IT & Software

  1. January 22nd, 2009 at 22:28 | #1

    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.

  2. February 9th, 2009 at 17:24 | #2

    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 :)

  1. No trackbacks yet.