Skip to content

Instantly share code, notes, and snippets.

@cjfrisz
Created January 11, 2014 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjfrisz/8375980 to your computer and use it in GitHub Desktop.
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
(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