Created
January 30, 2019 00:29
-
-
Save netpyoung/c407ea83ec8507379baedb029ca07191 to your computer and use it in GitHub Desktop.
return example
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
(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