Skip to content

Instantly share code, notes, and snippets.

@mikeananev
Last active July 30, 2022 05:06
Show Gist options
  • Save mikeananev/fd7bd00feee9a2ca0a10fb2c7b2d1325 to your computer and use it in GitHub Desktop.
Save mikeananev/fd7bd00feee9a2ca0a10fb2c7b2d1325 to your computer and use it in GitHub Desktop.
doto-cond macro
(defmacro ^:private doto-cond
"Example:
(doto-cond expr
cond1 (my call)
cond2 (my2 call2))
=>
(let [e# expr]
(when cond1 (my #e call))
(when cond2 (my2 #2 call2)))"
[expr & clauses]
(let [pairs (partition 2 clauses)
expr-sym (gensym "expr")]
`(let [~expr-sym ~expr]
~@(map (fn [[cond clause]]
`(when ~cond
(~(first clause) ~expr-sym ~@(rest clause))))
pairs)
~expr-sym)))
;; Useful for java obj -> doto operations
(let [{:keys [x y error-bars style]} data
{:keys [marker-color marker-type
line-color line-style line-width
fill-color show-in-legend?]} style]
(doto-cond
(if error-bars
(add-raw-series chart s-name x y error-bars)
(add-raw-series chart s-name x y))
marker-color (.setMarkerColor (colors marker-color marker-color))
marker-type (.setMarker (markers marker-type marker-type))
line-color (.setLineColor (colors line-color line-color))
line-style (.setLineStyle (strokes line-style line-style))
line-width (.setLineWidth (float line-width))
fill-color (.setFillColor (colors fill-color fill-color))
(not (nil? show-in-legend?)) (.setShowInLegend (boolean show-in-legend?))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment