Created
November 1, 2012 13:02
-
-
Save mnzk/3993505 to your computer and use it in GitHub Desktop.
multitable.clj : http://mnzk.hatenablog.com/entry/2012/10/31/221152
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns multitable | |
(:use [clojure.pprint :only (cl-format)])) | |
(defn make-row-seq | |
[deltas row] | |
(lazy-seq | |
(cons row (make-row-seq deltas (map + deltas row))))) | |
(defn make-table | |
[n m] | |
(let [heads (range n (inc m)) | |
row-1st (map #(* n %) heads) | |
rows (->> (make-row-seq heads row-1st) | |
(take (inc (- m n))))] | |
{:heads heads :rows rows})) | |
(defn to-output-seq | |
[{heads :heads rows :rows}] | |
(->> (map cons heads rows) | |
(cons (cons "" heads)))) | |
(defn write-table | |
([n m table] | |
(write-table n m table true)) | |
([n m table writer] | |
(let [width (apply max (map #(-> % str count) | |
[(* n n) (* m m) (* n m) n])) | |
fmt (format "~{~{~%d@a~^ ~}~:^\n~}" width)] | |
(cl-format writer fmt table)))) | |
(defn print-table | |
[n m] | |
(write-table n m (-> (make-table n m) | |
to-output-seq))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
seealso http://mnzk.hatenablog.com/entry/2012/10/31/221152