News for hackers. Stuff that matters.
Re:Brilliant or insane code? by scotty79 on Sunday September 01, 2013 @07:49AM (#6309980)

    i = iter(array)
    return zip(i, i, i)

There you go. All but neceessary magic gone with just one line more.
Re:Brilliant or insane code? by hartror on Sunday September 01, 2013 @07:53AM (#6309998)
A much clearer solution, I would also add a comment reminding people how zip works.
Re:Brilliant or insane code? by artursapek on Sunday September 01, 2013 @09:57AM (#6310367)
Really? What's the point in explaining a standard language function that they should know, and can just Google if they don't remember?
Re:Brilliant or insane code? by ekimekim on Sunday September 01, 2013 @10:20AM (#6310456)
It's using a somewhat-obscure guarantee that doesn't come up in normal usage of the function - namely, that it will always get the iterator values in left-right order.
Re:Brilliant or insane code? by andrewcooke on Sunday September 01, 2013 @10:35AM (#6310512)
it depends on the kind of code you write. i guess if you're writing web server stuff, documenting this makes sense. but in maths-related code, it's pretty standard. you use something very similar to transpose matrices, for example.

(and the original article is dealing with coords in graphics, which is "maths-related code" in my book, but perhaps not in everyone's)

Re:Brilliant or insane code? by loarake on Sunday September 01, 2013 @12:05PM (#6310888)
The original source is taken from a medical image DICOM viewer. In my limited experience as a medical physics student, the people working with these tools would really benefit from a comment explaining the code. They are most definitely not coders, most of them having barely done anything more than write a few matlab scripts.
Re:Brilliant or insane code? by duaneb on Monday September 02, 2013 @03:30AM (#6313900)
This is hardly a typical environment. All code would be challenging, even commented code. Excessive commenting would be necessary: write the program again in English.
Re:Brilliant or insane code? by jasonkeene on Sunday September 01, 2013 @10:09AM (#6310404)
Yeah, I was thinking the same thing. The only downside is that it isn't generic for variable length chunks.
Re:Brilliant or insane code? by jasallen on Sunday September 01, 2013 @09:58AM (#6310368)
It is clearer, but uses a fixed number of arguments, so may be unsuitable for some applications.
Re:Brilliant or insane code? by aidos on Sunday September 01, 2013 @04:50PM (#6312101)
The current function has the same restriction. In their case it's in the form of " * 3 ".
Re:Brilliant or insane code? by garethadams on Sunday September 01, 2013 @06:22PM (#6312413)
That `3` can be trivially replaced with a variable in a way that `(i, i, i)` cannot. But still, the refactoring of the iterator into a separate variable is the key step which reinforces that the same object is being passed 3 times
Re:Brilliant or insane code? by luikore on Monday September 02, 2013 @02:22AM (#6313706)
You don't have to use the same function to solve every problem.
Re:Brilliant or insane code? by slunk on Sunday September 01, 2013 @09:59AM (#6310373)
If you only ever need to chunk a list into 3-tuples, sure. If you want a general solution for chunking a list with zip and an iterator, this solution doesn't scale (look at hartror's and bjourne's posts for something that does).
Re:Brilliant or insane code? by obiterdictum on Sunday September 01, 2013 @10:27AM (#6310484)
I think that dependency on argument evaluation order inside zip function smells a bit.

It's OK here, but may bite you with a different function.

Re:Brilliant or insane code? by cbr on Sunday September 01, 2013 @12:03PM (#6310877)

    The left-to-right evaluation order of the
    iterables is guaranteed.
http://docs.python.org/2/library/functions.html#zip
Re:Brilliant or insane code? by weavejester on Sunday September 01, 2013 @03:58PM (#6311869)
Yes, but only for zip, which I believe was the parent posters point. With another function this technique might not work.
Re:Brilliant or insane code? by aidos on Sunday September 01, 2013 @05:04PM (#6312139)
That is infinitely more readable.
Re:Brilliant or insane code? by StavrosK on Sunday September 01, 2013 @07:51AM (#6309993)
Much, much better.