Skip to content

Instantly share code, notes, and snippets.

@joinr
Created March 29, 2024 03:10
Show Gist options
  • Save joinr/17819f1edab22db03b3a1bdad0abd419 to your computer and use it in GitHub Desktop.
Save joinr/17819f1edab22db03b3a1bdad0abd419 to your computer and use it in GitHub Desktop.
Meander experiments with vega
(require '[meander.epsilon :as m])
(require '[meander.strategy.epsilon :as m*])
(def simplify-conjunctions
(m*/rewrite
[:and_expr ?x] ?x
[:or_expr ?x] ?x))
(def simplify-all-conjs
(m*/bottom-up
(m*/attempt simplify-conjunctions)))
(defn as-geo [vega-lite-spec]
(-> vega-lite-spec
(update :encoding update-keys (fn [k]
(get {:x :latitude
:y :longitude
:x2 :latitude2
:y2 :longitude2}
k k)))
(assoc :projection {:type :mercator})))
(require '[meander.epsilon :as m])
(require '[meander.strategy.epsilon :as m*])
(def example
{:data {:url "data/cars.json"},
:width 400,
:height 300,
:background "floralwhite",
:encoding
{:x {:field "Horsepower", :type "quantitative"},
:y {:field "Miles_per_Gallon", :type "quantitative"},
:color {:field "Origin", :type "nominal"},
:tooltip
[{:field "Horsepower", :type "quantitative"}
{:field "Miles_per_Gallon", :type "quantitative"}]}})
(def cart->lat
(m*/rewrite
:x :latitude
:y :longitidue
:x2 :latitude2
:y2 :longitude2))
(def as-geo
(m*/bottom-up
(m*/attempt cart->lat)))
{:data {:url "data/cars.json"},
:width 400,
:height 300,
:background "floralwhite",
:encoding
{:latitude {:field "Horsepower", :type "quantitative"},
:longitidue {:field "Miles_per_Gallon", :type "quantitative"},
:color {:field "Origin", :type "nominal"},
:tooltip
[{:field "Horsepower", :type "quantitative"}
{:field "Miles_per_Gallon", :type "quantitative"}]}}
;;what if we need to point to slightly different fields?
;;convenience wrapper
(defmacro rewriting [& body]
`(->> (m*/rewrite ~@body) m*/attempt m*/bottom-up))
((rewriting :x :latitude
:y :longitidue
:x2 :latitude2
:y2 :longitude2)
example)
;;^equivalent to previous answer
;;slightly more complex transforms with arbitrary functions
(let [rename #(update % :field str "-2024")
f (rewriting {:field (m/some ?fld) :as ?m} (m/app rename ?m))]
(f example))
{:data {:url "data/cars.json"},
:width 400,
:height 300,
:background "floralwhite",
:encoding
{:x {:field "Horsepower-2024", :type "quantitative"},
:y {:field "Miles_per_Gallon-2024", :type "quantitative"},
:color {:field "Origin-2024", :type "nominal"},
:tooltip
[{:field "Horsepower-2024", :type "quantitative"}
{:field "Miles_per_Gallon-2024", :type "quantitative"}]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment