I have some mutually recursive code as follows:

(ns tester.core
  (:require [clojure.data.json :as json])
  (:gen-class))

(defn getValuePathPairs
  [json]
  (cond
    (instance? clojure.lang.PersistentVector json) (getVectorValuePathPairs 
json)
    (instance? clojure.lang.PersistentArrayMap json) (getMapValuePathPairs 
json)
    (instance? Boolean json) [[json ""]]
    (instance? Number json) [[json ""]]
    (instance? String json) [[json ""]])
)

(defn getVectorValuePathPairs
  [vec]
  (mapcat (fn [[i e]] 
         (for [[leaf-value path] (getValuePathPairs e)] 
           [leaf-value (str "[" i "]" path)]))
       (map-indexed vector vec))
)


(defn getMapValuePathPairs
  [m]
  (mapcat (fn [[k v]]
            (for [[leaf-value path] (getValuePathPairs v)]
              [leaf-value (str "[" k "]" path)]))
          m)
)

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println (getValuePathPairs ["Blah" 1 true])))

getValuePathPairs calls getVectorValuePathPairs and getMapValuePathPairs, 
which in turn call getValuePathPairs.  When I try to compile and run this 
code I get the following error:

Exception in thread "main" java.lang.RuntimeException: Unable to resolve 
symbol: getVectorValuePathPairs in this context, 
compiling:(tester/core.clj:8:52)


It appears from my experimentation that the issue is that when compiling 
getValuePathPairs, 
the function getVectorValuePathPairs isn't yet defined, as when I compile 
this into a CIDER repl with the recursion taken out, then compiled again 
with it put back it runs just fine.


Googling about mutual recursion in clojure provides a lot of hits about 
lack of tail call optimization, but no one saying that it's impossible to 
do (given short recursive depths).  In this particular piece of code, I 
could just inline the two inner functions, but I'd be giving up a lot of 
readability.  Is there a way to compile mutually recursive code?  If not, 
is there a good way to structure this sort of code so I can pull out small 
functions and name them without running into this problem?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to