Live data from Hacker News

Brilliant or insane code?

stavros.io

21–30 of 114 posts

Re: Brilliant or insane code?

#23
post #15
post #10

Earlier quoted context omitted.

I'd be surprised if my way is slower. Any time you unpack into a function such as zip() python has to create an intermediary list to store all the results before calling the function.

You could always use izip: http://docs.python.org/2/library/itertools.html#itertools.iz...

[deleted]

Re: Brilliant or insane code?

#24

It does not rely on an implementation detail, that is how iterators work. He's just supplied the same iterator to a function which consumes iterators... that's exactly the expected behaviour.

The fact that zip evaluates its arguments in order is an implementation detail. It could evaluate them in reverse order, in which case this code would not behave as expected.

That's what I meant, thanks for clarifying. It does seem that the zip() docs make a guarantee of left-to-right evaluation, though, so my apprehension proved unfounded.

Re: Brilliant or insane code?

#26
You can also do

    zip(arr[::3], arr[1::3], arr[2::3])
which is nearly as fast but doesn't work with iterators. If you want to use iterators you could also do

    zip(islice(arr, 0, None, 3), islice(arr, 1, None, 3), islice(arr, 2, None, 3))
which is a tad slower.

Re: Brilliant or insane code?

#27
post #9

We all love short and fast. But this is definitely an interesting approach. I'd love to see similar approaches to problems if you guys can point out to some.

I would also, I love this sort of thing. How about:

    >>> some_boolean = False
    >>> ["Thing 1", "Thing 2"][some_boolean]
    "Thing 1"

Re: Brilliant or insane code?

#28
post #5

It's not brilliant. This accomplishes the same thing without being hard to understand: from itertools import islice iterator = iter(array) try: while True: yield list(islice(iterator, 3)) except StopIteration: pass

This crashed my computer (it's an infinite loop that needs too much memory, apparently), and there's a mistake (the i is not defined).

Turns out that islice doesn't raise an IterationError, it just returns an empty list.

Fixing the problems, it runs in 237 μsec per loop, around 23 times more than the zip version.

Re: Brilliant or insane code?

#29
post #15
post #10

Earlier quoted context omitted.

I'd be surprised if my way is slower. Any time you unpack into a function such as zip() python has to create an intermediary list to store all the results before calling the function.

You could always use izip: http://docs.python.org/2/library/itertools.html#itertools.iz...

I was a bit wrong about that. For some reason I imagined that there would be quite a few args being passed to zip, when in fact there are just the three iterators. It does create a temporary list, but it's so small it's negligible. Using izip wouldn't really change anything.

Re: Brilliant or insane code?

#30
post #5

It's not brilliant. This accomplishes the same thing without being hard to understand: from itertools import islice iterator = iter(array) try: while True: yield list(islice(iterator, 3)) except StopIteration: pass

This crashed my computer (it's an infinite loop that needs too much memory, apparently), and there's a mistake (the i is not defined). Turns out that islice doesn't raise an IterationError, it just returns an empty list. Fixing the problems, it runs in 237 μsec per loop, around 23 times more than the zip version.

Haha. I feel very silly now. I was just about to respond with the same thing.

    while True:
        result = list(islice(iterator, 3))
        if not result:
            break
        yield result
Post reply on HN