Skip to content

Instantly share code, notes, and snippets.

@theanalyst
Last active December 28, 2015 05:59
Show Gist options
  • Save theanalyst/7453586 to your computer and use it in GitHub Desktop.
Save theanalyst/7453586 to your computer and use it in GitHub Desktop.
An attempt at writing some sequence utils in hy (heavily inspired by dash.el)
(defmacro inc! [var]
`(setv ~var (inc ~var)))
(defmacro --dotimes [n &rest body]
"Anaphoric form of dotimes; allows `it' to be used in the body for
eg. (--dotimes 10 (print it))"
`(let [[it 0]]
(while (< it ~n)
~@body
(inc! it))))
(defmacro -dotimes [n &rest body]
`(foreach [i (range ~n)] ~@body))
(defmacro -concat [item &rest coll]
"a buggy concat, works for strings and collections normally, will
break when you try something fancy in arguments like a function
that returns a list"
(if (all (map iterable? coll)) `(+ ~item ~@coll)
(raise (macro-error coll "invalid args..must be collections"))))
(defn mapcat [f &rest colls]
(let [[res []]]
(for [coll colls it coll]
(.extend res (f it)))
res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment