This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-25
Channels
- # announcements (21)
- # babashka (7)
- # beginners (27)
- # calva (7)
- # chlorine-clover (3)
- # cider (1)
- # clerk (21)
- # clojure (24)
- # clojure-europe (28)
- # clojure-finland (3)
- # clojure-nl (1)
- # clojure-norway (5)
- # clojure-uk (2)
- # clojurescript (13)
- # clr (2)
- # conjure (1)
- # consulting (1)
- # datahike (1)
- # datomic (13)
- # fulcro (3)
- # graalvm (33)
- # gratitude (7)
- # honeysql (7)
- # humbleui (12)
- # hyperfiddle (26)
- # interop (11)
- # introduce-yourself (4)
- # jobs-discuss (8)
- # lsp (26)
- # malli (6)
- # nbb (11)
- # polylith (26)
- # practicalli (1)
- # rdf (3)
- # re-frame (7)
- # reitit (10)
- # releases (2)
- # shadow-cljs (1)
- # tools-deps (15)
I have a sequential Clojure collection. I can call (.iterator coll)
on it to get an iterator, but how do I get a parameterized Java iterator, e.g. Iterator<Triple> in this case?
The coll already contains only Triple
objects, but I think the parameterized type of any Clojure collection is Object
. This doesn’t work when I need to call a Java method that takes Iterator<Triple>
as one arg.
Clojure collections are raw but you can cast them to the needed type. Or reify a Java interface that returns the generic type
> Clojure collections are raw but you can cast them to the needed type. How though…? Google is not helping… :S
Add ^Iterator
in front of the argument to that Java method. You can't add <Triple>
but you also don't need to.
@U2FRKM4TW How exactly would Java know which method to delegate to…? This is an overloaded method.
Ahhhhh, my bad, it actually isn’t, the method name is slightly different for the different parameterised types. I will try adding the type hint and see if it makes a diffeence.
Yeah, you can't have a method be overloaded on, say, Iterator<Triple>
and Iterator<T>
- you'll get a "name clash: methods have the same erasure" error from Java compiler.
Generics in Java are used only at compile time, after compilation information about generics is removed. https://en.wikipedia.org/wiki/Generics_in_Java#Problems_with_type_erasure
sorry, I assumed from the question that you were working in Java....
np @U064X3EF3, I was confused too!
SOLVED
Hello — macro puzzle of the day, here. I am processing vectors of naked int-symbol pairs to produce hashmaps, like this:
input:
[1 nym1, 2 nym2]
desired output:
({:stid 1, :nym nym1} {:stid 2, :nym nym2})
If I manually quote the symbols, I get the desired output:
(pprint (for [[stid nym]
;; MANUALLY QUOTED:
(partition 2 [1 'nym1, 2 'nym2])]
{:stid stid, :nym nym)))
;; => ({:stid 1, :nym nym1} {:stid 2, :nym nym2})
Manual quoting is not feasible for my big data sets, and preprocessing with, say, awk, is odious. So I try a macro! Results are inexplicably nil
:
(defmacro maps-of-pairs [naked-pairs]
(let [pairs (partition 2 naked-pairs)
fpairs (for [[stid nym] pairs]
`{:stid ~stid, :nym '~nym})]
`~fpairs))
;; NOT MANUALLY QUOTED:
(pprint (maps-of-pairs [1 nym1, 2 nym2]))
;; => nil
Where did I go wrong, please & thanks?Why would you write a macro? Just create a function and pass a quoted vector like this:
(defn by-chunks [n coll]
(partition n n [] coll))
(def data
'[1 foo 2 bar 3 baz])
(for [[int sym] (by-chunks 2 data)]
{:stid int :nym sym})
yes, that works better than trying to distribute the quotes to the symbols. Thanks. I have some more complications in my actual application context, but this is a good start at a solution.
Thad did it! SOLVED. Thanks again.
For future macrology reference:
(defmacro maps-of-pairs [naked-pairs]
(let [pairs (partition 2 naked-pairs)
fpairs (for [[stid nym] pairs]
`{:stid ~stid, :nym '~nym})]
`[~@fpairs])) ;; <==== remember, we are outputting code
;; NOT MANUALLY QUOTED:
(pprint (maps-of-pairs [1 nym1, 2 nym2]))
output:
[{:stid 1, :nym nym1} {:stid 2, :nym nym2}]
hth!another option for future readers, depending on the constraints of the larger picture, zipmap
can be used instead of for
here to simplify down to:
(def data '[1 nym1 2 nym2])
(map #(zipmap [:stid :nym] %)
(partition 2 data))
have used it a lot in the past for running similar transforms on csv data.oh very nice @UE4SAM0A0 !
(def !type (atom ["String"]) )
(get @!type 0)
;this is working if I give manually "String" instead of this method (get @!type 0).
(defn f [text]
(count (re-seq #(get @!type 0) text)))
(reduce + (into [] (map f (map :content vec)))) <--------- Why this is not working?