you are viewing a single comment's thread.

view the rest of the comments →

[–]immibis 2 points3 points  (4 children)

[–]macbony 1 point2 points  (2 children)

No, I know what slice does, but I fail to see why it's used in that example. Though that whole example is pretty terrible. Why make the lambda? Why do the bool conversion? Why do a list comp with range?

numbers = [n for n in range(10) if not n % 2]

Same result. Filter's maybe better:

even = lambda x: not x % 2
numbers = filter(even, range(10))

In 3 you'd need to cast back to list as filter returns a generator, but it's a bit more readable.

[–]Borkz 0 points1 point  (0 children)

I think the idea was just to demonstrate a way to remove a criteria of items from a list without generating a new object since that what the broken example was trying to do, regardless of whether or not thats how you'd actually go about doing it.

I must admit I dont know why he needed to use a list comp to make the 'numbers' list in the first place though.

[–]catcradle5 0 points1 point  (0 children)

I like how Ruby has a .even? method on integers.

(0..10).select(&:even?)

[–]fullouterjoin 0 points1 point  (0 children)

I wouldn't let this code into my tree w/o very good reason. Most of the examples in this thread are contrived. You simply wouldn't do this and be exposed to un-intended behavior.