Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created May 14, 2015 03:10
Show Gist options
  • Select an option

  • Save selfsame/9db890341c0c072c06aa to your computer and use it in GitHub Desktop.

Select an option

Save selfsame/9db890341c0c072c06aa to your computer and use it in GitHub Desktop.
(defmacro sandwich [col args & code]
"[col [prev current next] first-form 1-n-forms last-form]"
(let [top# (first code)
meat# (butlast (rest code))
bottom# (last code)]
`(remove nil? (apply concat (map-indexed
(fn [i# v#]
(let [~args [(get ~col (dec i#)) v# (get ~col (inc i#))]]
(cond (= i# 0) [~top# ~@meat#]
(= i# (dec (count ~col))) [~@meat# ~bottom#]
:else [~@meat#])))
~col)))))
(sandwich ['bread 'tomato 'lettuce 'bacon 'bread]
[before item after]
'olive
(when-not after 'mayo)
(if (= 'tomato before)
['soggy item]
item)
(when-not before 'mayo)
'paper-plate)
;(olive bread mayo tomato [soggy lettuce] bacon mayo bread paper-plate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment