Friday, October 16, 2009

Re: Python vs Clojure

Hi, it is NISHIO Hirokazu, a Python hacker.

In Python vs Clojure – Evolving « Best in Class I found a code:



What a mess!

Why you need icross. In Clojure it was written as (for [x (range 100 1000) y (range 100 1000)] (* x y)). It can be written (x * y for x in range(100, 1000) for y in range(100, 1000)) in Python too!

Why you need digits_from_num. In Clojure it was written as (= (seq s) (reverse s)). It can be written list(s) == list(reversed(s)) in Python too!!

And Why you need mul. In Clojure it was (* x y). Why you need new multiply function which takes arbitrary number of arguments?? Why didn't you write just (x * y)?

Finally, I concluded the messy python code is equivalent to the below:

print max(s for s in (x * y
for x in range(111, 1000)
for y in range(y, 1000))
if list(str(s)) == list(reversed(str(s))))


It is shorter than Clojure.



P.S. matz reported the original version of Python code takes 12.2 seconds, my version takes 4.9 seconds (both on Python2.6). And his ruby code (see first post) takes only 2.8 seconds on Ruby1.8.7 and 0.9 seconds on Ruby1.9.

http://twitter.com/yukihiro_matz/status/4901341641
http://twitter.com/yukihiro_matz/status/4901791131
http://twitter.com/yukihiro_matz/status/4901849726

No comments: