Fork me on GitHub
#beginners
<
2021-11-02
>
Dongwook Kim06:11:43

Hi, can I use type hint with record to function’s parameter?

(defrecord Foo [id name])

(defn prn-foo [^Foo f]
  (prn (:id f)))
above this is not working. But I wonder I can do this with another way or custom type(like defrecord or deftype) would not allow to use as type hint for parameter.

seancorfield06:11:05

What "is not working"? And why do you think you need the type hint?

Dongwook Kim06:11:05

(defrecord Foo [id name])
(defrecord Bar [id name])

(defn prn-foo [^Foo f]
  (prn (:id f)))

(comment
  (prn-foo (->Bar 1 "bar")))
I mean “not working” is In this case, I expect to get a warning when I run a rich-comment block. but it’s not. it runs without any warning or error. I think making a parameter object with defrecord can more express whole data structure in function signature than using map.

seancorfield06:11:03

Clojure isn't a typed language in that way. Records are mostly just maps. Both Foo and Bar have an id member so (:id f) will work for both.

seancorfield06:11:23

Type hints, for the most part, just affect Java interop to avoid reflection.

seancorfield06:11:53

Records are optimized maps that can participate in typed-dispatch, such as via protocols. See https://cemerick.com/blog/2011/07/05/flowchart-for-choosing-the-right-clojure-type-definition-form.html

Dongwook Kim06:11:55

hmm, I just want to express whole data structure in function signature using defrecord and type hint. I guess this approach is a wrong way then. Should find other way. thx!

seancorfield06:11:09

The recommended approach is "just use hash maps" -- unless you specifically need typed-dispatch.

seancorfield06:11:41

Clojure is a dynamically-typed language so you need to roll with that.

Dongwook Kim06:11:58

thanks for your advice, I’ll follow your recommend. 👍

1
andrejs09:11:25

Hello, need to convert data structure from {:name ["A" "B" "C"] :point [1 2 3]} to: [{:name "A" :point 1} {:name "B" :point 2} {:name "C" :point 3}] any ideas on how to do it? i am newbie and a little bit lost myself on this. Thanks

Fredrik09:11:07

There's many ways to do it, one way is to

(mapv #(hash-map :name %1 :point %2) (:name a) (:point a))

💜 1
Fredrik10:11:32

I came up with this as follows: We have two "sequential" things, the vectors ["A" "B" "C"] and [1 2 3], and I want to sort of line them up in a way that preserves order. Already this makes me think of using map (or mapv, which returns a vector instead), because it can go over multiple collections at a time. The anonymous function #(hash-map :name %1 :pont %2) gets two arguments at a time, one from each vector, creates the corresponding map, and then each such result is placed in a vector.

🙌 1
andrejs10:11:39

thank you very much!! all clear now!

😀 1
Mno12:11:31

There's probably a nice version with interleave

delaguardo15:11:37

#(zipmap (:name %) (:point %))

Fredrik16:11:17

How would you use that function to solve this problem?

Imdad Ahmed20:11:52

Hello, is there an emacs extension that provides a more human readable rendering of cider-errors?

dpsutton20:11:11

can you show a screenshot of what you are seeing? There was a lot of useful stuff in there. It can filter to just project stackframes, only clojure, etc.

dpsutton20:11:09

also, by their nature Clojure stacktraces endeavor to never lie, and show you exactly where the error happened. At first glance this can seem overwhelming but reading it carefully can be the best advice

Imdad Ahmed23:11:45

Sorry for the rather late reply. Here is a screenshot from cider:

Imdad Ahmed23:11:18

As opposed to a screenshot i took from babashka's output, which i found easier to parse:

Imdad Ahmed23:11:33

I'm trying to find if there are extensions available for emacs that would help getting the information out of that data in cider-error output

Imdad Ahmed23:11:39

No i haven't. Giving it a glance now, it seems to give me ability to change some default behaviours on where the stacktrace is shown (repl vs a new buffer). Also it gives you some key bindings to make it easier to navigate the stacktrace. I think this is certainly helpful, but may be what i'm looking for is someway to render the data from the error output that makes it more readable.