On Sun, Oct 25, 2009 at 10:08 PM, John Harrop <jharrop...@gmail.com> wrote:
> ReadingĀ http://www.paulgraham.com/power.html and specifically the section
> titled "Metrics" near the top, I realized that it would be very easy to
> calculate such metrics for Lisp code, and it took me literally only seconds
> to hack something in Clojure:
>
> user=> (defn count-nodes [data] (if (clojure.contrib.core/seqable? data) (+
> 1 (reduce + 0 (map count-nodes data))) 1))
> #'user/count-nodes
> user=> (count-nodes '(+ 3 (* 2 7)))
> 7

Fun!

> The count-nodes function itself has a code complexity of 22 by the same
> measure:
> user=> (count-nodes '(defn count-nodes [data] (if
> (clojure.contrib.core/seqable? data) (+ 1 (reduce + 0 (map count-nodes
> data))) 1)))

Well, speaking of higher-order functions and golf...

    (count-nodes
      '(defn count-nodes [data]
         (count (tree-seq clojure.contrib.core/seqable? seq data))))

    ; 12

Unfortunately I don't think contrib's sequable is quite what we want:

    (count-nodes "abcde")
    ; 6

This is a bit closer I think:

    (count-nodes
      '(defn count-nodes [data]
         (count (tree-seq (partial instance? clojure.lang.Seqable) seq data))))
    ; 15

...though preferring 'partial' over #() is of debatable merit.

--Chouser

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to