## 2009-10-16

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

Josh Robb said...

I'm probably misunderstanding - but shouldn't that be:

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

nathany said...

Much better.

The stack of parenthesis at the end is slightly amusing. :-)

NISHIO Hirokazu said...

>Josh Robb
Oh, thanks. I didn't care on that because I just wanted to show that Python is not such a bad language.

nathany said...

Python is a wonderful language, it's great that you can do stuff like this.

Javier said...

you have new opportunities to defend python from this dangerous newcomer ;-)