(ns example
  (:gen-class))

(defn abs [x]
  (if (> x 0)
    x
    (- x)))

(defn calc-benford-stats [digits]
  " Frequencies of digits in data "
  (let [y (frequencies digits)
         tot (reduce + (vals y))]
    [y tot]))

(defn show-benford-stats [v]
  " Prints in percent the actual, Benford expected, and difference"
  (let [fd (map (comp first str) v)]        
    (doseq [q (range 1 10)
            :let [[y tot] (calc-benford-stats fd)
                  d (first (str q))         
                  f (/ (get y d 0) tot 0.01)  
                  p (* (Math/log10 (/ (inc q) q)) 100)  
                  e (abs (- f p))]]                     
      (println (format "%3d%10.2f%10.2f%10.2f"
                       q
                       f
                       p
                       e)))))


(def fib (lazy-cat [0N 1N] (map + fib (rest fib))))


(def fib-digits (take 10000 fib))
(def header "         found-%    expected-%  diff")

(println "Fibonacci Results")
(println header)
(show-benford-stats fib-digits)


(println "Universal Constants from Physics")
(println header)
(let [
      data-parser (fn [s]
                  (let [x (re-find #"\s{10}-?[0|/\.]*([1-9])" s)]
                    (if (not (nil? x))    
                      (second x)
                      x)))

      input (slurp "http://physics.nist.gov/cuu/Constants/Table/allascii.txt")

      y (for [line (line-seq (java.io.BufferedReader.
                               (java.io.StringReader. input)))]
          (data-parser line))
      z (filter identity y)]
  (show-benford-stats z))


(println "Sunspots average count per month since 1749")
(println header)
(let [
      data-parser (fn [s]
                  (nth (re-find #"(.+?\s){3}([1-9])" s) 2))

      
      
      input (slurp "SN_m_tot_V2.0.txt")
      y (for [line (line-seq (java.io.BufferedReader.
                               (java.io.StringReader. input)))]
          (data-parser line))]

  (show-benford-stats y))