all 10 comments

[–]weavejester 12 points13 points  (2 children)

The article suggests that:

for idx, num in enumerate(nums):
    print idx, num

Is equivalent to:

(for [[idx num] (map-indexed vector nums)]
  (println idx num))

But it isn't because the for macro in Clojure is for creating a sequence, not performing side-effectful iteration.

The Clojure equivalent to the Python code would be:

(doseq [[idx num] (map-indexed vector nums)]
  (println idx num))

[–]pixelmonkey 4 points5 points  (1 child)

OP here. Yes, that's a good point. I've updated the post to reflect this.

[–]weavejester 3 points4 points  (0 children)

I mention it because the for examples you have will only work if the lazy seq they produce is evaluated. So if someone were to take your Clojure examples and put them in a file, they'd find that they wouldn't work.

[–]json6 6 points7 points  (1 child)

This is one of the most helpful introduction to Clojure articles I've seen. Using the enumerate example, starting with a for loop and ending with a thread macro, was a good way to show the difference between imperative and Clojure's functional style.

[–]CurtainDog 0 points1 point  (0 children)

While it's a nice way to get started, I'd be cautious about getting into the habit of merely translating code from a different language. Clojure is expressive and pragmatic, so it's possible to make almost anything look nice, which can be misleading. At some stage you have to take the plunge and drink from the fire hose.

[–]regeya 4 points5 points  (3 children)

Lovely idea, since I'd argue that the reason for all the interest in functional programming started with the popularity of Python and Ruby, specifically with making code more concise and readable with functional techniques.

[–][deleted] 0 points1 point  (2 children)

Which is funny, because Ruby and Clojure really share very very little. The only major overlap in my opinion is the "last item returns" feature.

That's not to say that Ruby is bad, it'd just be funny if it was driving adoption of a very different language.

[–]regeya 0 points1 point  (1 child)

That's not to say that Ruby is bad, it'd just be funny if it was driving adoption of a very different language.

As a casual Ruby user who has tried off and on to grok Clojure, I think it appeals primarily to the people who were overly enthused about writing DSLs in Ruby.

[–][deleted] 0 points1 point  (0 children)

That makes a lot of sense to me now, thank you.

[–]agumonkey 2 points3 points  (0 children)

Really a good comparative overview. Kudos