This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-03-14
Channels
- # announcements (2)
- # babashka (7)
- # beginners (20)
- # calva (2)
- # clj-kondo (7)
- # clojure (31)
- # clojure-australia (3)
- # clojure-dev (10)
- # clojure-europe (7)
- # clojurescript (13)
- # conjure (1)
- # cursive (27)
- # datomic (168)
- # events (3)
- # fulcro (2)
- # garden (3)
- # honeysql (2)
- # jobs (1)
- # joker (1)
- # lsp (52)
- # releases (1)
- # shadow-cljs (11)
- # slack-help (4)
- # spacemacs (22)
- # sql (1)
clojure.lang.IPersistentMap
(assocEx
[trie opath ovalue]
(if (get trie opath nil)
(throw (Exception. (format "Value for key %s already exists." opath)))
(assoc trie opath ovalue)))
Something about those lines is causing this error:
1. Caused by java.lang.ClassCastException
class clojure.lang.PersistentVector cannot be cast to class
java.util.Map$Entry (clojure.lang.PersistentVector is in unnamed module of
loader 'app'; java.util.Map$Entry is in module java.base of loader
'bootstrap')
core.clj: 1567 clojure.core/key
core_print.clj: 233 clojure.core/print-prefix-map/fn
core_print.clj: 66 clojure.core/print-sequential
core_print.clj: 229 clojure.core/print-prefix-map
core_print.clj: 238 clojure.core/print-map
Any ideas? The error seems to be only related to how the custom deftype prints to REPL. The code appears to otherwise work.Looks like a bogus .toString method, maybe in the call to format when building the exception, but that is not in the stacktrace
Oh, I should have included the line I was evaluating to cause the error.
(str (trie/->TrieAgain '() 1 {}))
doesn't raise.
(trie/->TrieAgain '() 1 {})
does raise.
ser=> (keys (reify clojure.lang.IPersistentMap (seq [_] (list [:a :b]))))
Error printing return value (ClassCastException) at clojure.lang.APersistentMap$KeySeq/first (APersistentMap.java:168).
class clojure.lang.PersistentVector cannot be cast to class java.util.Map$Entry (clojure.lang.PersistentVector is in unnamed module of loader 'app'; java.util.Map$Entry is in module java.base of loader 'bootstrap')
(user=> *e
#error {
:cause "class clojure.lang.PersistentVector cannot be cast to class java.util.Map$Entry (clojure.lang.PersistentVector is in unnamed module of loader 'app'; java.util.Map$Entry is in module java.base of loader 'bootstrap')"
:via
[{:type clojure.lang.ExceptionInfo
:message nil
:data #:clojure.error{:phase :print-eval-result}
:at [clojure.main$repl$read_eval_print__9112 invoke "main.clj" 442]}
{:type java.lang.ClassCastException
:message "class clojure.lang.PersistentVector cannot be cast to class java.util.Map$Entry (clojure.lang.PersistentVector is in unnamed module of loader 'app'; java.util.Map$Entry is in module java.base of loader 'bootstrap')"
:at [clojure.lang.APersistentMap$KeySeq first "APersistentMap.java" 168]}]
:trace
[[clojure.lang.APersistentMap$KeySeq first "APersistentMap.java" 168]
[clojure.lang.RT first "RT.java" 692]
[clojure.core$first__5402 invokeStatic "core.clj" 55]
[clojure.core$print_sequential invokeStatic "core_print.clj" 64]
[clojure.core$fn__7331 invokeStatic "core_print.clj" 174]
stacktrace doesn't entirely match, but pretty closeAhhh. So before I implemented the IPersistentMap methods... I was just getting the default random deftype representation... , like "com.owoga.trie.TrieAgain@6e7c56eb"
. But my REPL is now trying to print something that looks like a map, because it sees it's an instance of that. Gotcha. Thanks!
a tricky thing with keys is it returns a lazy seq, so exceptions may not be thrown until it is forced
in my example above the printing of the sequence returned by keys is forcing it and then throwing
(you can see the open paren dangling where it started to print the seq before it threw)
So my guess is something isn't right with the seq method, which is getting called when printing the result (not entirely sure what path through the printer is being triggered, that may be repl dependant)
Any hints on how to implement this in Clojure?
for i in range(800, 4000, 800):
for j in range(1, 65, 16):
print(i+j)
that doesn't seem the same @U2HBNQQBE, @UG703UB0T's version prints the sum of all the possible combinations, yours is a zipped sum