Fork me on GitHub

Hi, I am struggling to understand what is wrong with this piece of code, (.withBody (.withHeader (.withStatus (WireMock/aResponse) 200) "Content-Type" "application/json;charset=UTF-8") "{'abc': 'xyz'}"), 1. I get an exception - Execution error (ClassCastException) at ... java.lang.String cannot be cast to [Ljava.lang.String;, I do not understand why this exception comes. 2. I am trying to follow the first Java example here - 3. Generally how do you debug these cases


@ajay.rivendell The most likely cause is that one of the (Java) methods you're calling is variadic and has a signature with String... which actually means an array of strings, not zero or more strings.


Ok, let me check that ...


I'm assuming it's the with headers method


It would be a lot easier to figure out if they had a simple JavaDoc API website but I can't find that!


and I found this through


My link was to the actual source code.


Ok, got it now! Thank you! 👍:skin-tone-3:


So it needs to be (.withHeader (..) "Content-Type" (into-array String ["application/json;charset=UTF-8"]))


Horrible Java "builder" patterns! Ugh!

🙂 5

LMK if that works.


yes ... trying it now


it worked 🍻, thank you!


Can a transducer be made to take 1 element and split it into n number of elements and put them on the queue?


[{:a 1 :b 2}] -> [{:a 1} {:b 2}]

Alex Miller (Clojure team)05:02:32

user=> (into [] (mapcat #(map (fn [e] (apply hash-map e)) %)) [{:a 1 :b 2} {:c 3 :d 4}])
[{:a 1} {:b 2} {:c 3} {:d 4}]

Alex Miller (Clojure team)05:02:30

only top level is transducing here, inner is not


Nice solution. I should have expanded upon my expected output. The logic for splitting will need to be hard coded. Some keys in the map will duplicate (Specifically a timestamp & a device id) and some will split (different types of readings for the timestamp & device).


However it's good to know mapcat is what I need here

Alex Miller (Clojure team)05:02:06

yeah, mapcat is the main “expanding” transducer


I managed to do it with mapcat. the keys a & b will be hard coded so there is no way for me to simplify it further


@caleb.macdonaldblack You can parameterize the splitter like this

user=> (defn split-keys [ks] (apply juxt (map #(fn [m] (select-keys m [%])) ks)))
user=> ( (split-keys [:a :b]) {:a 1 :b 2} )
[{:a 1} {:b 2}]

juxt 5

So you can define your splitter function for any set of keys, and then do

    (map splitter)
    (mapcat identity))
  [{...} {...}])


And you can get your total sequence of unique keys to feed to split-keys by (set (mapcat keys [{...} {...}]))


@seancorfield That splitter is a great idea. However, i need some keys not specified to stay. So I modified your solution to the following which works for me now

(defn split-keys [ks]
  (apply juxt 
      #(fn foo [m]
         (select-keys m (cons % (set/difference (set (keys m)) ks))))
((split-keys [:a :b]) {:a 1 :b 2 :c 3})
=> [{:a 1, :c 3} {:b 2, :c 3}]


So ultimately I end up with something like this. I'm really happy with how this turned out

(defn- ->device-event-row [id timezone]
  (fn [{:keys [data.interval/datetime] :as row}]
    (let [[k v] (first (dissoc row :data.interval/datetime))
          [read-type uom] (str/split (name k) #"-")
          uom (str/upper-case uom)]
      {:timestamp datetime
       :value     v
       :read-type read-type
       :uom       uom
       :timezone  timezone
       :device-id id})))

(def batch-n 1000)

(defn split-keys [ks]
  (apply juxt
      #(fn foo [m]
         (select-keys m (cons % (set/difference (set (keys m)) ks))))

(def row-splitter
  (split-keys [:data.interval/energy-wh :data.interval/demand-kva :data.interval/power-w]))

(defn batch-rows [{:keys [device/id device/timezone]} meter-data]
  (let [->device-event-row (->device-event-row id timezone)]
        (map row-splitter)
        (mapcat identity)
        (map ->device-event-row)
        (partition-all batch-n))


Then I'm going to batch those into a database


Clojure is so wonderful for data transformation 🙂

👍 25
Karol Wójcik09:02:14

Is there a way to gen-class which implements generic interface with some types?


generics don't exist, you can implement any interface


that is, the type parameter is a fiction, it's enforced by the java compiler but on the bytecode level (where clojure operates), there's nothing

Raymond Ko20:02:42

Hello, all. Is the current consensus that you should avoid using

due to it not working in ClojureScript and to avoid symbol conflicts?

Lennart Buit20:02:38

I think in general you want to have control over what and what you are not requiring


I personally avoid :use and :refer :all because it hurts understandability of code.

👍 5

With a short namespace alias in the :require form, I save typing while still making it immediately evident where a function/var came from.

Lennart Buit20:02:49

(it also can break reloading of code without restarting your repl)

Lennart Buit20:02:23

funny, I started to make more namespace aliases longer lately

Lennart Buit20:02:39

the few-letter-acronyms became too much


At work, some of the namespace aliases were used so often that a single letter sufficed. But other namespaces were rarer, so I used the longer form.


I like using aliases because I can jump to the top of the file and immediately learn what namespace is being referenced. Then what to call them, for me, comes down to what’s idiomatic in a project.


So I like both, depending on context :)


Also, with aliases, if your editor has live code completion, when you type (nsa/ it will offer up all the (public) symbols in whatever namespace nsa is an alias for. Harder for it to do that with used/`refer`ed names.

Lennart Buit21:02:07

also, cursive doesnt appear to discover unused require/refers

Raymond Ko21:02:21

Yeah, I agree with all of this. Been using Joker to do linting, and it warns against this.


Big +1 for Joker! Very handy for tracking unused stuff and weirdly constructed code.

Raymond Ko21:02:38

What is in the nsa namespace? 😉


@raymond.w.ko It's meant to indicate "some arbitrary ns alias in your code"

Raymond Ko21:02:55

Wow, I just realized how accurate the "It's just data" meme is for my own question...


(I typed various things before just giving up and pressing enter with nsa in the code)


When writing ->> expressions, is there a simple way to print out intermediate values in the chain for debugging purposes? I’ve found myself using let instead to print out intermediate bindings, in cases where a thread macro would be a perfect fit

Lennart Buit21:02:05

(map (fn [v] (println v) v))

Lennart Buit21:02:10

or actually what someone else is saying 😛


Greetings! If I have a return value of [(1 2) (21 22) (41 42)], what would be the best way to get each index of each element into its own result, so it becomes [(1 21 41) (2 22 42)]?


@risinglight there’s probably a better way, but I just define a trace function like

(defn trace [thing]
  (prn thing)
and stick it in the middle of the stack


(apply map vector [[1 2] [21 22] [41 42]]) ;; => ([1 21 41] [2 22 42])


@dpsutton wow, I was making that much too complicated. Thank you!


the trick is map can take many collections. be aware of what happens when their lengths are different


play in the repl and get it working for you

👍 5

@manutter51 I actually wrote a trace function just like that, but I used it to wrap one of the intermediate expressions (causing an arity error from the macro inserting another argument to trace), instead of just inserting the function itself into the pipeline… I feel like I’m not nearly smart enough to write Clojure most days 🙁


I know the feeling, but I had it a lot worse when my day job was writing Java 😉


Clojure seems to be getting easier with practice more than Java ever did though. For me at least.


I guess that’s the hope… keep banging head against problem until it hurts less 🙂

Lennart Buit21:02:35

perseverance is such a great skill ^^


How do I :require a method from a defrecord? I have a namespace with a function that takes a record and calls a method on the record.


the method function belongs to the namespace that defines the protocol


if it's an interface and not a protocol, there's a method but no function

Ryan Haskell-Glatz23:02:05

I know this isn't the world's most impressive program, but I just made my first Clojure thing!

👏 25
🙌 10
Ryan Haskell-Glatz23:02:01

Hope you folks like guessing games 😅


meta-game: try to make the game run out of stack before guessing your number :D


with some refactoring it could use recur instead of self-call of guess, but realistically I think overflow is unlikely

👍 5

you'd have to be really bad at the game 🙂


nice work ryan

☺️ 5