Live data from Hacker News

Brilliant or insane code?

stavros.io

11–20 of 114 posts

Re: Brilliant or insane code?

#11
post #6
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

Sure, there are many ways to do it, but I think the author was going for speed here.

Did you test and time the itertools version?

Re: Brilliant or insane code?

#12

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.

In fact official python docs say:

"The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip([iter(s)]n)."

http://docs.python.org/2/library/functions.html#zip

Re: Brilliant or insane code?

#13

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.

Re: Brilliant or insane code?

#14
post #10
post #6

Earlier quoted context omitted.

Sure, there are many ways to do it, but I think the author was going for speed here.

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.

Exactly, I would be very surprised if the zip version was faster. One of the first steps to optimization in python is moving everything you can to generators and using of itertools.

The OP's question of is this genius or bad is clear in that regard: it is bad, due to not being the proper optimization direction, but it is interesting.

Re: Brilliant or insane code?

#15
post #10
post #6

Earlier quoted context omitted.

Sure, there are many ways to do it, but I think the author was going for speed here.

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...

Re: Brilliant or insane code?

#17
This is in the zip documentation as the way of solving this problem. Sort of surprised the author didn't look up the documentation before writing what is otherwise a very good post.

    The left-to-right evaluation order of the iterables is guaranteed. This makes
    possible an idiom for clustering a data series into n-length groups using
    zip(*[iter(s)]*n).
http://docs.python.org/2/library/functions.html#zip

Re: Brilliant or insane code?

#18
like dict(zip( or dict(getmembers(asdf)).keys(), it's idiomatic code. it wouldn't have occurred to me the first time i had to write such a function, but now that i've taken a few moments to read the article i find it clearer than the list comprehension version (because the constant only appears once) and nicer than the numpy version in that it doesn't require an extra dependency.

may save a few keystrokes some rainy day. good post.

Re: Brilliant or insane code?

#19
post #17

This is in the zip documentation as the way of solving this problem. Sort of surprised the author didn't look up the documentation before writing what is otherwise a very good post. The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n). http://docs.python.org/2/library/functions.html#zip

I am also surprised I didn't look it up. I guess that I didn't expect that anything more than how zip() works would be there, and I already knew that.

I've updated the post with this, another commenter pointed it out. Thanks!

Post reply on HN