Out of simple curiosity I wondered how hard it would be to implement flow
control using proxy. I know Rich isn't hot on non-structured programming,
but there may be times where this might be useful:
(ns flow.return-from
(:import (flow IReturnFrom)))
(defn create-return-from
[value]
(proxy [Throwable IReturnFrom] []
(value [& args] value)))
(defmacro allow-return-from [form]
`(try
~form
(catch ~'clojure.proxy.java.lang.Throwable$IReturnFrom e#
(.value e#))))
(defmacro return [value]
`(throw (create-return-from ~value)))
(defn my-loop [count]
(dotimes [x count]
(if (= x 5)
(return x))))
;; examples
(allow-return-from (my-loop 10)) ; -> 5
(dotimes [x 5]
(allow-return-from
(dotimes [y 5]
(if (= y 2)
(return y)
(println x y)))))
In order for the above to work you'll need to compile an interface:
;; compile with the following
;; (binding [*compile-path* "/path/to/classes"]
;; (compile 'flow.return-from-interface))
(ns flow.return-from-interface)
(gen-interface
:name flow.IReturnFrom
:methods [[value [] Object]])
Pretty cool.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---