for (Iterator it = container.iterator(); it.hasNext(); )
if (!predicate(it.next())(
it.remove();
It's more ugly and error prone if you've got to juggle an index, though. for (Iterator it = container.iterator(); it.hasNext(); )
if (!predicate(it.next())(
it.remove();
It's more ugly and error prone if you've got to juggle an index, though. for (i = ctr_size(container); i > 0; i--)
if (!predicate(container, i - 1))
ctr_remove(container, i - 1); for (i = ctr_size(container); i--; )
if (!predicate(container, i))
ctr_remove(container, i); filter predicate xs
:)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.
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.var filteredCollection = collection.Where(x => predicate(x));
var filteredCollection = collection.Where(predicate);
I agree that C# is a fundamentally usable language. for (i = container.size; i-->0;)
if(deletep(container, i))
container.remove(i); while(container.Size > 0)
container.Remove(0);
No need for variables. container.clear();
No need for looping :)The iterator may become invalid if the collection it derived from changes.
Iterator delete methods are crazy to begin with since iteration does not correlate with deletion. But anyway it is not clear where the iterator's cursor will point after you delete the current element. You could end up deleting every second element in the container.
In the systems class I TAed, when students had memory corruption problems, removing items while iterating over a linked list was at the top of my list of things to look for.