clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
2020-11-12T00:12:56.372100Z

Does “no matching ctor” indicate a constructor w/ the same argument length doesn’t exist or also check the types as well?

alexmiller 2020-11-12T00:35:46.372400Z

can be either

2020-11-12T04:29:37.374500Z

Tangent from my prior question yesterday, I realized I could probably build myself the aliasing system I might want. So I'm still thinking about this, but as I've been struggling to figure out how to structure my Specs for entities, what name to give them, and what names to give my keys, I think this is a potentially interesting option:

(def key-ns
  (atom {}))

(defn defkey
  [alias keyns]
  (swap! key-ns assoc (str alias) (str keyns)))

(set! *default-data-reader-fn*
      (fn[tag-sym value]
        (when (.startsWith (str tag-sym) "key")
          (if (map? value)
            (let [tag-key-str (second (re-find #"key:(.*)" (name tag-sym)))]
              (if-let [tag-key (@key-ns tag-key-str)]
                (reduce (fn[acc [k v]]
                          (if (qualified-keyword? k)
                            (assoc acc k v)
                            (assoc acc (keyword tag-key (name k)) v)))
                        {} value)
                (throw (ex-info (str "No keyword namespace defined for " value) {}))))
            (let [ns (namespace value)
                  na (name value)]
              (if-let [k (@key-ns (str (or ns na)))]
                (keyword (name k) na)
                (throw (ex-info (str "No keyword namespace defined for " value) {}))))))))

2020-11-12T04:50:14.376600Z

Now with this, I can nicely define entities and entity spec with logical namespaces for my Specs and keywords, which are not tied to the physical code namespace they happen to be defined in:

(defkey 'person 'com.myorg.app.person)

(s/def #key person/name
  string?)

(s/def #key person/address
  string?)

(s/def #key person
  (s/keys :req [#key person/name
                #key person/address]))

#key:person {:name "John Doe"
             :address "222 2nd Ave., New York, New York, USA"
             :some/other 100}

alexmiller 2020-11-12T13:17:26.384100Z

It seems like you’ve just recreated aliases, namespace map syntax

alexmiller 2020-11-12T13:18:59.385700Z

Aliases are the right answer to this, they just have the downside right now of requiring real loadable namespaces

valerauko 2020-11-12T10:43:30.378500Z

If I know that a thing is a symbol, is there a way to get the metadata of the var pointed to by the symbol? Something like

(when (symbol? x)
  (meta (var (??? x))))

borkdude 2020-11-12T10:50:44.378700Z

@vale resolve

1👍1💯
valerauko 2020-11-12T10:51:12.378900Z

thanks!

zilti 2020-11-12T10:59:56.380700Z

What is the most elegant way to turn

[{:company "A" :accounts [{:name "A"} {:name "B"} {:name "C"}]} ...]
into
[{:company "A" :account {:name "A"}} {:company "A" :account {:name "B"}} ...]
?

borkdude 2020-11-12T11:03:22.380900Z

@zilti

(for [{:keys [:company :accounts]} [{:company "A" :accounts [{:name "A"} {:name "B"} {:name "C"}]}]
      account accounts]
  {:company company
   :account account})

zilti 2020-11-12T11:11:04.382Z

Hmm. Thanks! I was hoping for something more elegant (maybe something as "obscure" as juxt) that could be used, but I guess that'll do fine then.

borkdude 2020-11-12T11:12:34.382300Z

I don't see how it can be more elegant than this to be honest ;P

2✔️
zilti 2020-11-12T11:13:48.383Z

I am not sure what I imagined or hoped for 😛 Just thought it's worth a shot to ask

alexmiller 2020-11-12T13:17:26.384100Z

It seems like you’ve just recreated aliases, namespace map syntax

alexmiller 2020-11-12T13:18:59.385700Z

Aliases are the right answer to this, they just have the downside right now of requiring real loadable namespaces

mbjarland 2020-11-12T16:01:51.387100Z

umm, I have a lengthy clojure api design(ish) question, are half-pagers ok here or is there a more appropriate channel for this?

dpsutton 2020-11-12T16:03:07.388200Z

i think feedback is always better when you can ask a very concise question with a problem statement that is reduced to the minimal example. whether that takes a half a page is up to the problem. but a question that long probably won't get as much time and responses from others

borkdude 2020-11-12T16:06:43.388600Z

@mbjarland Maybe #code-reviews is also appropriate, I'm not sure

seancorfield 2020-11-12T16:13:32.390100Z

@mbjarland There's also #architecture which is often more long-form...

mbjarland 2020-11-12T16:14:16.391300Z

Thanks @dpsutton, @borkdude, and @seancorfield - I will try those.

acim1 2020-11-12T16:14:59.392Z

Does anyone know what reporter or other option I would use to get play-by-play test output with kaocha? That is, I want to see the test description and result as it's going, not just a number of failed passed at the end.

borkdude 2020-11-12T16:16:13.392300Z

@mkeller02 there's a #kaocha channel for that

acim1 2020-11-12T16:20:38.393100Z

Thanks @borkdude, FYI I think I rubber ducked this one...what I needed was --reporter kaocha.report/documentation

1👍
borkdude 2020-11-12T21:12:58.395Z

I'm running into an issue with tools.reader.edn. For reasons I need to be able to pass a LineNumberingPushbackReader for reading. This got me into the following issue:

user=> (require '[clojure.tools.reader.edn :as tre])
nil
user=> (tre/read (clojure.lang.LineNumberingPushbackReader. (java.io.StringReader. "(+)")))
Execution error (IOException) at java.io.PushbackReader/unread (PushbackReader.java:155).

borkdude 2020-11-12T21:13:39.395700Z

Any ideas why this happens? I can read "(1 2 3 4)" fine, it's the + which seems to trigger this

borkdude 2020-11-12T21:15:10.396200Z

The same issue happens with a "normal" pushbackreader:

user=> (tre/read (java.io.PushbackReader. (java.io.StringReader. "(+)")))
Execution error (IOException) at java.io.PushbackReader/unread (PushbackReader.java:155).
Pushback buffer overflow

2020-11-12T21:29:43.396600Z

I can replicate for +, -, but not for other symbols/numbers, I bet this is a buggy definition or precedence for number parsing

Daniel Stephens 2020-11-12T21:37:13.397300Z

this seems to work,

(tre/read (clojure.tools.reader.reader-types/string-push-back-reader "(+)")) 
though I don't know what the difference between the readers is yet

borkdude 2020-11-12T21:39:49.398Z

yep that works. but that's not what I need. the docstring states: > Reads the first object from an IPushbackReader or a http://java.io.PushbackReader.

1👍
Daniel Stephens 2020-11-12T21:41:11.398900Z

(tre/read {:eof nil} (java.io.PushbackReader. (java.io.StringReader. "(+)") 2))
seemingly the buffer size of 1 is too small here, I'm not certain on what dictates the length needed but this works

alexmiller 2020-11-12T21:46:45.400500Z

1 character is not enough to determine if + is a number or a symbol ?

borkdude 2020-11-12T21:48:54.401300Z

Also normal tools.reader works:

user=> (tr/read (java.io.PushbackReader. (java.io.StringReader. "(+)")))
(+)
Just the tools.reader.edn barfs on this

borkdude 2020-11-12T22:10:29.402200Z

I might have a fix:

user=> (tre/read (java.io.PushbackReader. (java.io.StringReader. "(+)")))
(+)

borkdude 2020-11-12T22:11:34.402400Z

I'll make an issue + patch.

borkdude 2020-11-12T22:21:27.402900Z

Patch: code + test https://clojure.atlassian.net/browse/TRDR-63

borkdude 2020-11-12T22:24:29.403400Z

@alexmiller Should I assign it to someone?

alexmiller 2020-11-12T22:24:46.403600Z

no

borkdude 2020-11-12T22:24:58.403800Z

ok

alexmiller 2020-11-12T22:26:11.404800Z

the repro is good there but I can't tell what the actual cause or the proposed solution is

borkdude 2020-11-12T22:26:22.405Z

ok, I'll explain in the issue

alexmiller 2020-11-12T22:27:29.405300Z

https://clojure.atlassian.net/browse/CLJ-1187 is a random example of what I try to do in CLJ tickets

borkdude 2020-11-12T22:28:26.405600Z

I'll probably do that tomorrow, I'm pretty tired now :)

alexmiller 2020-11-12T22:28:30.405800Z

no worries at all

alexmiller 2020-11-12T22:28:45.406100Z

just a plea from your friendly neighborhood project maintainer

1👍
borkdude 2020-11-12T23:24:20.406800Z

@alexmiller Now added explanation