diff options
author | Rich Hickey <richhickey@gmail.com> | 2008-11-25 16:06:13 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2008-11-25 16:06:13 +0000 |
commit | 18fcdd50ffe1ba3f9b44020c83525123a75e6a25 (patch) | |
tree | c47336014ca531f2bc8efeacef52c83e0ee4feec /src | |
parent | 10541c0254973ce045305499d4b5622073ede622 (diff) |
fixed doc string on trampoline, added support for passing args
Diffstat (limited to 'src')
-rw-r--r-- | src/clj/clojure/core.clj | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index d0b040d4..b896754c 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -3493,18 +3493,21 @@ "defs the supplied var names with no bindings, useful for making forward declarations." [& names] `(do ~@(map #(list 'def %) names))) -(defn trampoline [f] +(defn trampoline "trampoline can be used to convert algorithms requiring mutual - recursion without stack consumption. f must be a fn of no - arguments. Calls f. If f returns a fn, calls that fn with no - arguments, and continues to repeat, until the return value is not a - fn, then returns that non-fn value. Note that if you want to return - a fn as a final value, you must wrap it in some data structure and - unpack it after trampoline returns." - (let [ret (f)] - (if (fn? ret) - (recur ret) - ret))) + recursion without stack consumption. Calls f with supplied args, if + any. If f returns a fn, calls that fn with no arguments, and + continues to repeat, until the return value is not a fn, then + returns that non-fn value. Note that if you want to return a fn as a + final value, you must wrap it in some data structure and unpack it + after trampoline returns." + ([f] + (let [ret (f)] + (if (fn? ret) + (recur ret) + ret))) + ([f & args] + (trampoline #(apply f args)))) |