Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roughly how many functions are in the Clojure core libraries?

Common Lisp has over 700 functions in its core libraries. Roughly how many does Clojure have in its libraries?

(Where 'core library' is defined as everything you get access to when you include [org.clojure/clojure "1.5.1"] in your project.clj)

like image 618
hawkeye Avatar asked Dec 15 '22 08:12

hawkeye


1 Answers

(Update: Inspired by this question, I have released Varspotting, a Leiningen plugin and library for producing reports on data of this kind. The reports for Clojure 1.5.1 are displayed in the README; see also my comment below for the numbers. The minor differences between those counts and the ones in the answer below are a result of the REPL environment including certain bindings which Varspotting omits from its counts.)

Using Clojure 1.5.1.

The clojure.core namespace:

  1. Public Vars:

    (count (ns-publics 'clojure.core))
    ;= 591
    
  2. Public Vars which hold non-macro functions (the filter function is meant to exclude the 6 Vars which hold maps, see below for how to find them; there are not sets or vectors to exclude at this time):

    (->> (ns-publics 'clojure.core)
         vals
         (filter #(not (.isMacro %)))
         (map deref)
         (filter (every-pred ifn? (comp not map?)))
         count)
    ;= 477
    
  3. With #(.isMacro %) in the filter above, we find there are 76 public macros.

  4. The 32 public Vars which do not hold function-like values can be discovered with this snippet:

    (->> (ns-publics 'clojure.core)
         vals
         (remove (comp ifn? deref)))
    
  5. Use (filter (comp map? deref)) instead of the (remove ...) in the above to find the 6 Vars holding map values.

Same as 2. for all namespaces in the Clojure jar (skipping the deprecated clojure.parallel and additionally excluding the two Vars holding vectors; there are no sets to exclude at this time; hopefully haven't missed anything else):

(dorun (map require '[clojure.core clojure.data clojure.edn clojure.inspector
                      clojure.instant clojure.java.browse clojure.java.javadoc
                      clojure.java.io clojure.java.shell
                      clojure.main clojure.pprint clojure.reflect clojure.repl
                      clojure.set clojure.stacktrace clojure.string
                      clojure.template clojure.test clojure.walk clojure.xml
                      clojure.zip]))

(->> (mapcat ns-publics
             '[clojure.core
               clojure.data
               clojure.edn
               clojure.inspector
               clojure.instant
               clojure.java.browse
               clojure.java.javadoc
               clojure.java.io
               clojure.java.shell
               clojure.main
               clojure.pprint
               clojure.reflect
               clojure.repl
               clojure.set
               clojure.stacktrace
               clojure.string
               clojure.template
               clojure.test
               clojure.walk
               clojure.xml
               clojure.zip])
     vals
     (filter #(not (.isMacro %)))
     (map deref)
     (filter (every-pred ifn? (comp not map?) (comp not vector?)))
     count)
;= 676

For macros, the total count is 99.

like image 93
Michał Marczyk Avatar answered Jan 04 '23 23:01

Michał Marczyk