Skip to content

Instantly share code, notes, and snippets.

@collinalexbell
Created January 19, 2017 22:59
Show Gist options
  • Save collinalexbell/c1680151a67904ca426087ed82123906 to your computer and use it in GitHub Desktop.
Save collinalexbell/c1680151a67904ca426087ed82123906 to your computer and use it in GitHub Desktop.
(defmacro infix [a b c]
`(~b ~a ~c))
(defmacro python-fn-call [f a]
(if (not (nil? a))
(cons f a)
f))
(defmacro python [l]
(let [for-index (.indexOf l 'for)
inj-clause (subvec l 0 for-index)
in-index (.indexOf l 'in)
fn-clause (subvec l (+ 1 in-index))
]
`(map (fn [~(first inj-clause)]
~(if (= 3 (count inj-clause))
`(infix ~(first inj-clause) ~(second inj-clause) ~(nth inj-clause 2))
(first inj-clause)))
(python-fn-call ~(first fn-clause) ~(nth fn-clause 1 nil)))))
;;tests
(infix 2 * 2)
(python-fn-call range(10))
(python [x * 2 for x in range(10)])
(python [x * 3 for x in range (10)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment