Last active
August 28, 2023 09:45
-
-
Save humorless/5953dd892e3f110ed28b534e67ad685d to your computer and use it in GitHub Desktop.
Demo of treewalk
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 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}]]]]] | |
) |
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 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