Re:Brilliant or insane code?
by scotty79 on Sunday September 01, 2013 @07:49AM
(#6309980)
There you go. All but neceessary magic gone with just one line more.
[ Parent
| on HN ]
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?
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)
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.
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.
It is clearer, but uses a fixed number of arguments, so may be unsuitable for some applications.
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
You don't have to use the same function to solve every problem.
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.
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.
That is infinitely more readable.