Note that there are some explanatory texts on larger screens.

plurals
  1. POIs functional Clojure or imperative Groovy more readable?
    text
    copied!<p>OK, no cheating now.</p> <p>No, really, take a minute or two and try this out.</p> <p>What does "positions" do?</p> <p>Edit: simplified according to cgrand's suggestion.</p> <pre><code>(defn redux [[current next] flag] [(if flag current next) (inc next)]) (defn positions [coll] (map first (reductions redux [1 2] (map = coll (rest coll))))) </code></pre> <p>Now, how about this version?</p> <pre><code>def positions(coll) { def (current, next) = [1, 1] def previous = coll[0] coll.collect { current = (it == previous) ? current : next next++ previous = it current } } </code></pre> <p>I'm learning Clojure and I'm loving it, because I've always enjoyed functional programming. It took me longer to come up with the Clojure solution, but I enjoyed having to <i>think</i> of an elegant solution. The Groovy solution is alright, but I'm at the point where I find this type of imperative programming <i>boring</i> and mechanical. After 12 years of Java, I feel in a rut and functional programming with Clojure is the boost I needed.</p> <p>Right, get to the point. Well, I have to be honest and say that I wonder if I'll understand the Clojure code when I go back to it months later. Sure I could comment the heck out of it, but I don't need to comment my Java code to understand it.</p> <p>So my question is: is it a question of getting more used to functional programming patterns? Are functional programming gurus reading this code and finding it a breeze to understand? Which version did <i>you</i> find easier to understand?</p> <p>Edit: what this code does is calculate the positions of players according to their points, while keep track of those who are tied. For example:</p> <pre><code> Pos Points 1. 36 1. 36 1. 36 4. 34 5. 32 5. 32 5. 32 8. 30 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload