welcome: please sign in
SmartList

Concept

When you're crafting algorithms that have to modify a list, it's often easier to output a new list than modify the existing list since modifying the existing list requires you use a counter and set an element at the counter. Here's an example of writing a filter that includes all elements that are divisible by the next element in the list:

Is that not pretty? (Answer: Yes, that is not pretty.)

Here's a much prettier way of doing it:

Wow! It runs inline, and it's much shorter. Of course, I had to invert the logic and call remove, but it's pretty nice still. :)

List comprehensions benefit even more:

Yes, you're not doing it inline, but it's pretty, isn't it?

API

SmartList class:

SmartElement class:

More Examples

Remove duplicates from a list

SmartList (last edited 2010-04-16 17:09:11 by localhost)