beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
Eric Ihli 2021-03-14T02:56:02.456800Z

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.

2021-03-14T02:59:22.457800Z

What about that stacktrace makes you think the error is coming from that line?

2021-03-14T03:00:41.459400Z

Looks like a bogus .toString method, maybe in the call to format when building the exception, but that is not in the stacktrace

Eric Ihli 2021-03-14T03:04:28.459700Z

When I comment those lines out, I don't get the error.

Eric Ihli 2021-03-14T03:05:18.460400Z

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.

2021-03-14T03:07:57.461200Z

it looks like you are printing a map where keys doesn't return a seq of map entries

2021-03-14T03:11:44.462300Z

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 close

Eric Ihli 2021-03-14T03:12:21.462900Z

Ahhh. 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!

2021-03-14T03:12:37.463100Z

maybe, not entirely sure

2021-03-14T03:13:24.463900Z

a tricky thing with keys is it returns a lazy seq, so exceptions may not be thrown until it is forced

2021-03-14T03:13:46.464500Z

in my example above the printing of the sequence returned by keys is forcing it and then throwing

2021-03-14T03:14:07.465Z

(you can see the open paren dangling where it started to print the seq before it threw)

2021-03-14T03:19:27.467800Z

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)

ArtW 2021-03-14T16:23:39.473700Z

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)

william 2021-03-14T16:35:08.473800Z

(doseq [x (range 800 4000 800)
        y (range 1 65 16)]
  (prn (+ x y)))

william 2021-03-14T16:36:04.474Z

and lookup the difference between doseq and for

ArtW 2021-03-14T17:02:47.474200Z

Thanks!

agigao 2021-03-14T18:20:07.474900Z

(map + (range 800 4000 800) (range 1 65 16))

william 2021-03-14T19:08:23.475300Z

that doesn't seem the same @chokheli, @art.bikes's version prints the sum of all the possible combinations, yours is a zipped sum

ArtW 2021-03-14T19:28:02.475500Z

Correct, I was trying to display a list of screen locations for an application I am writing in TUTOR.