Fork me on GitHub
#specter
<
2018-05-09
>
soulflyer05:05:24

I'm seeing a difference in the behaviour of a specter recursive-path example (found on stack overflow) in the clojure repl and the clojurescript repl. It works as expected in clojure but throws an error in clojurescript. In clojure I get this:

user> (def data
  {:items [{:name "Washing machine"
            :subparts [{:name "Ballast" :weight 1}
                       {:name "Hull" :weight 2}]}]}) 
#'user/data
user> (require '[com.rpl.specter :as specter])
nil
user> (specter/select
  [(specter/recursive-path [] p
     [(specter/walker :name) (specter/stay-then-continue [:subparts p])])
   :name]
  data)
["Washing machine" "Ballast" "Hull"]
user> user> (def data
  {:items [{:name "Washing machine"
            :subparts [{:name "Ballast" :weight 1}
                       {:name "Hull" :weight 2}]}]})
#'user/data
user> (require '[com.rpl.specter :as specter])
nil
user> (specter/select
  [(specter/recursive-path [] p
     [(specter/walker :name) (specter/stay-then-continue [:subparts p])])
   :name]
  data)
["Washing machine" "Ballast" "Hull"]
user> 
whereas the same code tried under clojurescript throws an error Unable to resolve var p:
anh-front.tree> (require '[com.rpl.specter :as specter])
nil
anh-front.tree> (def data
  {:items [{:name "Washing machine"
            :subparts [{:name "Ballast" :weight 1}
                       {:name "Hull" :weight 2}]}]})
#'anh-front.tree/data
anh-front.tree> (specter/select
  [(specter/recursive-path [] p
     [(specter/walker :name) (specter/stay-then-continue [:subparts p])])
   :name]
  data)
----  Could not Analyze  <cljs form>   line:1  column:1  ----

  Unable to resolve var: p in this context at line 1 <cljs repl>

  1                (specter/select
                   ^--- 
  2  [(specter/recursive-path [] p
  3     [(specter/walker :name) (specter/stay-then-continue [:subparts p])])
  4   :name]
  5  data)

----  Analysis Error  ----
nil
anh-front.tree> 
Is there something special needed to get recursive-path to play nicely with clojurescript?

nathanmarz12:05:30

@soulflyer does it work if you refactor the recursive-path definition out of the select with a let or def?

souenzzo13:05:55

Doing a cljsbuild with advanced

May 09, 2018 1:13:26 PM com.google.javascript.jscomp.LoggerErrorManager printSummary
WARNING: 1 error(s), 0 warning(s)
ERROR: JSC_DUPLICATE_PARAM. Parse error. Duplicate parameter name "G__13210_13216" at /workspace/my-cool-project/target/cljsbuild-compiler-0/com/rpl/specter/impl.js line 543 : 32
[31mCompiling ["resources/public/javascript/main.js"] failed.[0m
java.lang.Exception: Closure compilation failed
It's a random error. Tryied to build again and it passes. Has anyone else ever had this problem? Using: [com.rpl/specter "1.1.0"] [org.clojure/clojurescript "1.10.238"] https://github.com/nathanmarz/specter/blob/1.1.0/src/clj/com/rpl/specter/impl.cljc#L543

soulflyer13:05:21

@nathanmarz yes, seems to work fine if I use def.