News for hackers. Stuff that matters.
Re:What's wrong with this code, really? by JoachimSchipper on Friday September 16, 2011 @11:01AM (#3004693)
Even with plain C, you can just iterate backwards:

    for (i = ctr_size(container); i > 0; i--)
        if (!predicate(container, i - 1))
            ctr_remove(container, i - 1);
Re:What's wrong with this code, really? by WildUtah on Friday September 16, 2011 @01:03PM (#3005277)
Use the goes-to-zero operator.

  for (i = container.size; i-->0;)
    if(deletep(container, i))
      container.remove(i);
Re:What's wrong with this code, really? by romac on Friday September 16, 2011 @02:27PM (#3005646)
Reminds me of something :)
Re:What's wrong with this code, really? by hxa7241 on Friday September 16, 2011 @12:05PM (#3004998)

    for (i = ctr_size(container); i--; )
        if (!predicate(container, i))
            ctr_remove(container, i);
Re:What's wrong with this code, really? by JoachimSchipper on Friday September 16, 2011 @12:20PM (#3005069)
That's arguably better. Nitpick: start at ctr_size(container) - 1. [Feel free to edit your post, and I'll just delete this one.]
Re:What's wrong with this code, really? by BrandonM on Friday September 16, 2011 @03:12PM (#3005841)
Nope. The decrement is occurring in the test, so it occurs after the initialization and before the first iteration.
Re:What's wrong with this code, really? by sid0 on Friday September 16, 2011 @12:33PM (#3005125)

  filter predicate xs
:)
Re:What's wrong with this code, really? by rtperson on Friday September 16, 2011 @01:30PM (#3005401)
Haskell, I assume. Two things really make Haskell shine when it comes to problems like this: 1) higher-order list operations -- you're not iterating through indices, so there is no possibility for off-by-one errors, and 2) immutability -- your code takes a new list with the undesirable elements removed; you can't remove elements of a structure at the same time you're iterating through it.

I always love the problems on SPOJ where they give you the number of cases up front, because in Haskell you can almost always throw out that value. Your map function knows when the list is out of elements.

Re:What's wrong with this code, really? by sid0 on Friday September 16, 2011 @01:37PM (#3005434)
Yeah, I was thinking Haskell, but any functional language will do. Problems like the one in the blog post really make you appreciate them.
Re:What's wrong with this code, really? by morsch on Friday September 16, 2011 @03:00PM (#3005793)
The snippet I posted can easily be made into a generic function<T> that takes a collection<T> and a predicate<T>. For example, the Apache Commons library offers such a function for Java, so where available the code comes down to the fairly similar

  CollectionUtils.filter(collection, predicate);
Of course aside from being a bit more verbose, predicate needs to be an object (often a singleton), because you can't pass around functions.
Re:What's wrong with this code, really? by wanorris on Friday September 16, 2011 @03:20PM (#3005877)
It's not as concise as Haskell, but the addition of FP capabilities to C# make it a lot more pleasant as a practical language than it was in earlier days.

var filteredCollection = collection.Where(x => predicate(x));

Re:What's wrong with this code, really? by sid0 on Friday September 16, 2011 @03:34PM (#3005939)
As far as I remember you can write that as

  var filteredCollection = collection.Where(predicate);
I agree that C# is a fundamentally usable language.
Re:What's wrong with this code, really? by MostAwesomeDude on Friday September 16, 2011 @10:38PM (#3007067)
l = [i for i in l if predicate(i)]
Re:What's wrong with this code, really? by Flow on Saturday September 17, 2011 @05:55AM (#3007674)

  while(container.Size > 0)
    container.Remove(0);
No need for variables.
Re:What's wrong with this code, really? by huhtenberg on Saturday September 17, 2011 @12:09PM (#3008348)

  container.clear();
No need for looping :)
Re:What's wrong with this code, really? by Flow on Saturday September 17, 2011 @12:41PM (#3008422)
Yeah sure, :) , but I meant for those other cases when there are no Clear() method. :)