Skip to content

Instantly share code, notes, and snippets.

@netpyoung
Created January 30, 2019 00:29
Show Gist options
  • Save netpyoung/c407ea83ec8507379baedb029ca07191 to your computer and use it in GitHub Desktop.
Save netpyoung/c407ea83ec8507379baedb029ca07191 to your computer and use it in GitHub Desktop.
return example
(ns example
(:require
[cloroutine.core :refer [cr]]))
;; ref: https://github.com/leonoel/cloroutine
(def ^:dynamic *tail*)
(defn gen-seq [gen]
(binding [*tail* (lazy-seq (gen-seq gen))]
(gen)))
(defn return [x]
(cons x *tail*))
(defn no-op [] nil)
(defmacro generator [& body]
(let [cnt (count body)
[front back] (split-at (dec cnt) body)]
`(gen-seq (cr {return no-op} (do ~@front (return ~@back)) nil))))
(defmacro defn-x [name args & body]
`(defn ~name ~args
(-> (generator
~@body)
(first))))
(defn-x helloworld [a]
(when (even? a)
(return a))
:b)
(helloworld 10)
;;=> 10
(helloworld 9)
;;=> :b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment