dimanche 30 décembre 2012

ASCII printing of Clojure's datastructures

Printing Clojure's datastructures as graphs using vijual


cljsta.graph> (draw-tree [[:north-america [:usa [:miami]
                                                [:seattle]
                                                [:idao [:boise]]]]
                          [:europe [:germany]
                                   [:france [:paris]
                                            [:lyon]
                                            [:cannes]]]])
        +------------+        +--------+
        |    north   |        | europe |
        |   america  |        +---+----+
        +-----+------+            |
              |         +---------+---------+
              +         |                   |
              |    +----+----+          +---+----+
           +--+--+ | germany |          | france |
           | usa | +---------+          +---+----+
           +--+--+                          |
              |                    +--------+---------+
    +---------++---------+         |        |         |
    |          |         |     +---+---+ +--+---+ +---+----+
+---+---+ +----+----+ +--+---+ | paris | | lyon | | cannes |
| miami | | seattle | | idao | +-------+ +------+ +--------+
+-------+ +---------+ +--+---+
                         |
                         +
                         |
                     +---+---+
                     | boise |
                     +-------+
nil

Pretty printing tables using Clojure core lib

cljsta.graph> (clojure.pprint/print-table [{:a 1 :b 2 :c 3} {:b 5 :a 7 :c "dog"}])

=============
:a | :c  | :b
=============
1  | 3   | 2
7  | dog | 5
=============
nil

Pretty printing tables using cldwalker / table


cljsta.graph> (table (meta #'doc))
+-----------+---------------------------------------------------------------+
| key       | value                                                         |
+-----------+---------------------------------------------------------------+
| :macro    | true                                                          |
| :ns       | clojure.repl                                                  |
| :name     | doc                                                           |
| :arglists | ([name])                                                      |
| :added    | 1.0                                                           |
| :doc      | Prints documentation for a var or special form given its name |
| :line     | 120                                                           |
| :file     | clojure/repl.clj                                              |
+-----------+---------------------------------------------------------------+
nil

Pretty printing tables using joegallo / doric

https://github.com/joegallo/doric

cljsta.lib-example.joegallo-table> (println (table [{:a 1 :b 2}]))
|---+---|
| A | B |
|---+---|
| 1 | 2 |
|---+---|
nil

It even have barcharts! (I love this one):

cljsta.lib-example.joegallo-table> (defn trunc [x] (double (/ (int (* 100 x))
                                                              100)))
#'cljsta.lib-example.joegallo-table/trunc
cljsta.lib-example.joegallo-table> (defn gauss [x] (/ (Math/exp (- (* 1/2 x x)))
                                                      (Math/sqrt (* 2 Math/PI)))
                                                      )
#'cljsta.lib-example.joegallo-table/gauss
cljsta.lib-example.joegallo-table> (println (table ^{:format raw}
                                                 [:x {:name :y :format bar}]
                                                 (map (fn [x] {:x (trunc x) :y (int (* 80 (gauss x)))}) (range -2.8 2.8 0.15))
                                                 ))
  X                  Y
-2.8
-2.65
-2.5  #
-2.35 ##
-2.2  ##
-2.05 ###
-1.9  #####
-1.75 ######
-1.6  ########
-1.45 ###########
-1.3  #############
-1.15 ################
-1.0  ###################
-0.85 ######################
-0.7  ########################
-0.55 ###########################
-0.4  #############################
-0.25 ##############################
-0.1  ###############################
0.04  ###############################
0.19  ###############################
0.34  ##############################
0.49  ############################
0.64  #########################
0.79  #######################
0.94  ####################
1.09  #################
1.24  ##############
1.39  ###########
1.54  #########
1.69  #######
1.84  #####
1.99  ####
2.14  ###
2.29  ##
2.44  #
2.59  #
2.74
nil







2 commentaires:

  1. Hi,

    what is also pretty cool with the lib joegallo/doric and cldwalker/table is that we can use the output to use in a org-mode file :D

    tony

    RépondreSupprimer
    Réponses
    1. Hi Tony,

      Yes you are right, both have options to output org-mode style tables.
      In fact doric can directly output HTML markup.

      I didn't described all the options, as it would have been a *much* larger post.

      Denis

      Supprimer

Membres