This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-02-07
Channels
- # announcements (35)
- # beginners (80)
- # boot (1)
- # calva (4)
- # cider (33)
- # cljdoc (40)
- # clojars (3)
- # clojure (95)
- # clojure-berlin (2)
- # clojure-europe (4)
- # clojure-italy (28)
- # clojure-nl (2)
- # clojure-seattle (1)
- # clojure-serbia (1)
- # clojure-spec (74)
- # clojure-uk (71)
- # clojurescript (29)
- # core-async (1)
- # cursive (80)
- # data-science (4)
- # datomic (17)
- # duct (75)
- # emacs (4)
- # figwheel-main (5)
- # fulcro (3)
- # jackdaw (1)
- # java (1)
- # jobs-discuss (20)
- # off-topic (32)
- # parinfer (2)
- # pathom (23)
- # re-frame (26)
- # reagent (25)
- # rum (6)
- # shadow-cljs (122)
- # speculative (4)
- # sql (17)
- # testing (7)
- # yada (8)
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 - http://wiremock.org/docs/getting-started/
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.
It would be a lot easier to figure out if they had a simple JavaDoc API website but I can't find that!
Found it! Yup, .withHeader
https://github.com/tomakehurst/wiremock/blob/master/src/main/java/com/github/tomakehurst/wiremock/client/ResponseDefinitionBuilder.java#L95
right, the ones I am checking is not variadic - https://jar-download.com/javaDoc/com.github.tomakehurst/wiremock/2.3.1/com/github/tomakehurst/wiremock/client/ResponseDefinitionBuilder.html
My link was to the actual source code.
So it needs to be (.withHeader (..) "Content-Type" (into-array String ["application/json;charset=UTF-8"]))
LMK if that works.
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}]
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}]
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
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
user=> ( (split-keys [:a :b]) {:a 1 :b 2} )
[{:a 1} {:b 2}]
user=>
So you can define your splitter function for any set of keys, and then do
(eduction
(comp
(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
(map
#(fn foo [m]
(select-keys m (cons % (set/difference (set (keys m)) ks))))
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
(map
#(fn foo [m]
(select-keys m (cons % (set/difference (set (keys m)) ks))))
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)]
(eduction
(comp
(map row-splitter)
(mapcat identity)
(map ->device-event-row)
(partition-all batch-n))
meter-data)))
Is there a way to gen-class which implements generic interface with some types?
Maybe gen-interface
is what you need?https://clojuredocs.org/clojure.core/gen-interface
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
Hello, all. Is the current consensus that you should avoid using
(:use)
in (ns)
due to it not working in ClojureScript and to avoid symbol conflicts?I think in general you want to have control over what and what you are not requiring
An influential guide: https://stuartsierra.com/2016/clojure-how-to-ns.html
I personally avoid :use
and :refer :all
because it hurts understandability of code.
With a short namespace alias in the :require
form, I save typing while still making it immediately evident where a function/var came from.
(it also can break reloading of code without restarting your repl)
funny, I started to make more namespace aliases longer lately
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.
agreed ^^!
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 use
d/`refer`ed names.
also, cursive doesnt appear to discover unused require/refers
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.
What is in the nsa
namespace? 😉
@raymond.w.ko It's meant to indicate "some arbitrary ns alias in your code"
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
(map (fn [v] (println v) v))
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)
thing)
and stick it in the middle of the stack@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
@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.
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
@noisesmith thanks
I know this isn't the world's most impressive program, but I just made my first Clojure thing!
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