Skip to content

Instantly share code, notes, and snippets.

@humorless
Last active August 28, 2023 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humorless/5953dd892e3f110ed28b534e67ad685d to your computer and use it in GitHub Desktop.
Save humorless/5953dd892e3f110ed28b534e67ad685d to your computer and use it in GitHub Desktop.
Demo of treewalk
(ns myproject
"FIXME: my new org.corfield.new/scratch project."
(:require [clojure.walk :as walk]))
(def lines
(list {:reference-number "invoice 1"
:status "unreleased"}
(list {:invoice "invoice 1"
:account "100"
:desp "customer invoice line"
:tax-amount 2000
:id 1}
(list {:invoice "inovice 1"
:tax-amount 200
:id 1}
(list {:invoice "inovice 1"
:tax-amount 200
:id 2}
(list {:invoice "inovice 1"
:tax-amount 200
:id 3}))))))
(defn hiccup-tag
[ele]
(cond
(some? (:reference-number ele)) :customer-invoice
(some? (:desp ele)) :customer-invoice-line
:else :customer-invoice-tax-line))
(defn f
[x]
(if (list? x)
(let [ele (first x)
tag (hiccup-tag ele)]
(vec (conj x tag)))
x))
(walk/postwalk f lines)
(comment
[:customer-invoice
{:reference-number "invoice 1", :status "unreleased"}
[:customer-invoice-line
{:invoice "invoice 1",
:account "100",
:desp "customer invoice line",
:tax-amount 2000,
:id 1}
[:customer-invoice-tax-line
{:invoice "inovice 1", :tax-amount 200, :id 1}
[:customer-invoice-tax-line
{:invoice "inovice 1", :tax-amount 200, :id 2}
[:customer-invoice-tax-line
{:invoice "inovice 1", :tax-amount 200, :id 3}]]]]]
)
(ns myproject
"FIXME: my new org.corfield.new/scratch project."
(:require [clojure.walk :as walk]))
(def lines
(list {:reference-number "invoice 1"
:status "unreleased"}
(list {:invoice "invoice 1"
:account "100"
:desp "customer invoice line"
:tax-amount 2000
:id 1}
(list {:invoice "inovice 1"
:tax-amount 200
:id 1}
{:invoice "inovice 1"
:tax-amount 200
:id 2}
(list {:invoice "inovice 1"
:tax-amount 200
:id 3})))))
(defn hiccup-tag
[ele]
(cond
(some? (:reference-number ele)) :customer-invoice
(some? (:desp ele)) :customer-invoice-line
:else :customer-invoice-tax-line))
(defn f
[x]
(if (list? x)
(let [ele (first x)
tag (hiccup-tag ele)]
(vec (conj x tag)))
x))
(walk/postwalk f lines)
(defn -main
"Invoke me with clojure -M -m myproject"
[& args]
(println "-main with" args))
(comment
[:customer-invoice
{:reference-number "invoice 1", :status "unreleased"}
[:customer-invoice-line
{:invoice "invoice 1",
:account "100",
:desp "customer invoice line",
:tax-amount 2000,
:id 1}
[:customer-invoice-tax-line
{:invoice "inovice 1", :tax-amount 200, :id 1}
{:invoice "inovice 1", :tax-amount 200, :id 2}
[:customer-invoice-tax-line
{:invoice "inovice 1", :tax-amount 200, :id 3}]]]]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment