Live data from Hacker News

Brilliant or insane code?

stavros.io

41–50 of 114 posts

Re: Brilliant or insane code?

#41

Earlier quoted context omitted.

I would also, I love this sort of thing. How about: >>> some_boolean = False >>> ["Thing 1", "Thing 2"][some_boolean] "Thing 1"

Yeah, but unlike the hack in the article, the more readable version of that code, "Thing 1" if some_boolean else "Thing 2" is also almost twice as fast(775 vs 1340 ns, on my machine).

I was more posting it for the "cute hack" value, rather than speed. Speed was significant in the post because the author needed their library to be fast.

Re: Brilliant or insane code?

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

You might like the Python Infrequently Answered Questions: http://norvig.com/python-iaq.html (Even if it's a bit old.)

Re: Brilliant or insane code?

#43
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"

Short but I doubt this will be faster as noted.

Re: Brilliant or insane code?

#44
post #37

I'm glad Clojure has top-level support for this operation... it's quite flexible too, and the presence of partition-all makes it explicit what you should expect if the sequence doesn't evenly partition. user=> (partition 3 [1 2 3 4 5 6]) ((1 2 3) (4 5 6)) user=> (partition 3 [1 2 3 4 5 6 7]) ((1 2 3) (4 5 6)) user=> (partition-all 3 [1 2 3 4 5 6 7]) ((1 2 3) (4 5 6) (7)) user=> (partition 3 3 (repeat 0) [1 2 3 4 5 6…

can you measure the speed and post it here ?

Re: Brilliant or insane code?

#46
post #39

Earlier quoted context omitted.

I would also, I love this sort of thing. How about: >>> some_boolean = False >>> ["Thing 1", "Thing 2"][some_boolean] "Thing 1"

the problem with this expression is that it doesn't "short circuit". Both "Thing 1" and "Thing 2" (in this cases object, but the can be function calls) are evaluated before the "some_boolean" usage.

That can be an advantage in some cases, however. Not generally, though.

In particular, if you want the side effects of both operations. Trying to write multiple things while checking if any of them failed, for instance.

Re: Brilliant or insane code?

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

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.

That is a common misconception. Moving to iterators adds a function call while list creation in C is quite fast. Every case has to be tested for performance.

Re: Brilliant or insane code?

#49
post #37

I'm glad Clojure has top-level support for this operation... it's quite flexible too, and the presence of partition-all makes it explicit what you should expect if the sequence doesn't evenly partition. user=> (partition 3 [1 2 3 4 5 6]) ((1 2 3) (4 5 6)) user=> (partition 3 [1 2 3 4 5 6 7]) ((1 2 3) (4 5 6)) user=> (partition-all 3 [1 2 3 4 5 6 7]) ((1 2 3) (4 5 6) (7)) user=> (partition 3 3 (repeat 0) [1 2 3 4 5 6…

Haskell also has it in the Data.List.Split library, which is handy, although it's easy to define yourself.

λ> chunksOf 3 [1..12]

[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]

Re: Brilliant or insane code?

#50
post #7

I think the code is pretty ok, except for the stupid name, docstring and that it is a method and not a free function. def chunks(seq, n): "groups the elements of the seq into a list of n-sized chunks." return zip(*[iter(seq)]*n)

The original docstring is a complete red herring... yours is much better, but I'd go one further and include an example.
Post reply on HN