Skip to content

Instantly share code, notes, and snippets.

@markomanninen
Created August 28, 2017 08:35
Show Gist options
  • Save markomanninen/f2e3637f285112f87845cd5863197e10 to your computer and use it in GitHub Desktop.
Save markomanninen/f2e3637f285112f87845cd5863197e10 to your computer and use it in GitHub Desktop.
; index-find function needs to be available at compile time for the lambda expression macro
(eval-and-compile
; set comma constant for separator
; at the moment Hy doesn't support dot on macro expressions because dot is mixed with internal HyCons functionality
; causing this error: https://github.com/hylang/hy/blob/e8ffd412028232cc2cc4fe4bfb10f21ce8ab2565/hy/compiler.py#L2478
(setv comma '·)
; find index of the element from the list. if the element is not found, return -1
(defn index-find [elm lst]
(try (.index lst elm) (except [ValueError] -1))))
; lambda expression macro
; for example: (𝜆 x y · x 1 0) ; -> 1
(defmacro 𝜆 [&rest expr]
(setv idx (index-find comma expr))
`(fn ~(cut expr 0 (if (pos? idx) idx 0)) ~@(cut expr (inc idx))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment