Fork me on GitHub
#clojure
<
2020-11-12
>
colinkahn00:11:56

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

didibus04:11:37

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) {}))))))))

didibus04:11:14

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}

Alex Miller (Clojure team)13:11:26

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

Alex Miller (Clojure team)13:11:59

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

valerauko10:11:30

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))))

borkdude10:11:44

@vale resolve

💯 3
👍 3
zilti10:11:56

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"}} ...]
?

borkdude11:11:22

@zilti

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

zilti11:11:04

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.

borkdude11:11:34

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

✔️ 6
zilti11:11:48

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

mbjarland16:11:51

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

dpsutton16:11:07

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

borkdude16:11:43

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

seancorfield16:11:32

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

mbjarland16:11:16

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

acim116:11:59

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.

borkdude16:11:13

@mkeller02 there's a #kaocha channel for that

acim116:11:38

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

👍 3
borkdude21:11:58

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).

noisesmith21:11:43

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

borkdude21:11:39

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

borkdude21:11:10

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

Daniel Stephens21:11:13

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

borkdude21:11:49

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.

👍 3
Daniel Stephens21:11:11

(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

Alex Miller (Clojure team)21:11:45

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

borkdude21:11:54

Also normal tools.reader works:

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

borkdude22:11:29

I might have a fix:

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

borkdude22:11:34

I'll make an issue + patch.

borkdude22:11:29

@alexmiller Should I assign it to someone?

Alex Miller (Clojure team)22:11:11

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

borkdude22:11:22

ok, I'll explain in the issue

Alex Miller (Clojure team)22:11:29

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

dominicm03:11:45

I felt bad when I saw your edits to my jira, because I'd done such a poor job. Not sure if there's a template feature, but I'd fill this out without forgetting if it was there by default!

borkdude22:11:26

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

Alex Miller (Clojure team)22:11:45

just a plea from your friendly neighborhood project maintainer

👍 3
borkdude23:11:20

@alexmiller Now added explanation