Created
January 11, 2014 20:01
-
-
Save cjfrisz/8375980 to your computer and use it in GitHub Desktop.
Example of CPSing variadic Clojure fn expression with compatibility for non-CPS calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fn [w x y & z] z) | |
(let [empty-k (with-meta identity {:kont true})] | |
(fn foo | |
;; need a compatibility overload for if someone called | |
;; using only required parameters. | |
([w x y] (foo empty-k w x y)) | |
;; acts as its own compatibility layer, since we can't | |
;; have another variadic overload | |
([k w x y & z] | |
(if (:kont (meta k)) | |
(k z) | |
;; funny thing to note about clojure semantics here: | |
;; when using `recur` with a variadic function, the | |
;; last argument to the recur must explicitly be a list. | |
;; you can't simply tack on extra arguments as with a | |
;; non-recur call | |
(recur empty-k k w x (conj z y)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment