Fork me on GitHub

Trying to wrap my head around using core.async in clojurescript to get around an ajax callback in a reagent template, any advice on places to look?


There are channels for #reagent #clojurescript and #core-async that might be able to help (but things tend to be pretty quiet on a Sunday)


ok, cool, I think I may have figured out what I needed for now, but I'll keep those in mind the next time I have questions


Hey everyone, is there a canonical formatting and or linting used in the clojure{script} world? I have done a lot of Golang, and in Go, formatting and basic linting is provided by the complier, which silences all the arguments. I'd love to just use the "generally accepted" formatting and linting right off the bat


I can recommend clj-kondo as a “generally accepted” linter, and this blog post about formatting will explain why there is no such tool (at least not “generally accepted” 🙂 ) -


Ok, thanks @U04V4KLKC thats very helpful


Hi, I need a hand with interop... In Java I have following scenario: I have a builder with a bunch of methods on it. It has signatures with varargs and there are also overrides like this:

Builder someAction(String s, Operation... ops);
Builder someAction(Pattern p, Operation... ops);
I didn't find a way to call any of these from Clojure. Is there a way how to hint types in such case? Operation[] hint would be handful, but I don't know how to declare it.


Use (into-array Operation [ ...operation instances... ])


🆒 into-array was exactly what I was looking for 🙂 I tried make-array and object-array but it's clearly wrong because target type is then Object[] Thanks Alex! It would be great if there is an example in the faq page below.


sounds like a good idea



👍 4
💯 4

(.someAction builder "foo" (into-array Operation [])) - like that

Ramon Rios16:12:52

Is there a way for execute two side effects on a function?


(defn effects [] (fire-missiles) (delete-all-users) (utter-chaos!))


not sure what you're looking for but you can use a do expression. And defn's have an implicit do body

Ramon Rios17:12:50

I would like to have to call two assoc-in in a function

Ramon Rios17:12:56

To set 2 different values

Ramon Rios17:12:25

I'm asking it cause in my lein midje autotest i see that it's only recognizing the last assoc on the function that i'm calling


if you have something like this

(let [foo {:a 1}]
  (assoc foo :b :2)
  (assoc foo :c 3))
you aren't changing foo but making a new value. So the first assoc call creates a map with the b key on it but that doesn't change foo.
(let [foo {:a 1}]
  (-> foo
      (assoc :b :2)
      (assoc :c 3)))
Compare that to this threaded version


exactly. this is saying "take foo, make a copy of it with the b key as two, and then to that copy put the c key.

Ramon Rios17:12:18

This is a macro, right?

Ramon Rios17:12:09

I saw it a lot on java or other functional stuff, but the concept of this and ->> doens't fit on my mind


yes its a macro


and a quite popular one in clojure


which is equivalent to (assoc (assoc {:a 1} :b 2) :c 3)

Ramon Rios17:12:54

(defn check-correspondence [db]
  (assoc-in db [::current :policy-correspondence] :prod-manager)
  (assoc-in db [::current :invoicing-correspondence] :prod-manager))

Ramon Rios17:12:05

That´s my code


yup. common mistake. Clojure's data structures are immutable. So in your first assoc-in call you don't keep that value so it is effectively thrown away


Read you statements as "to a copy of db, add the prod-manager under policy-correspondence. to a different copy of db, add prod-manager at invoicing-correspondence"

Ramon Rios17:12:55

I think now it's main main challange, to refactor my way of think for functional and immutable data

Ramon Rios17:12:19

And how should be the right way for do it?


try those two different forms i put above in a repl


Can we yield a map from an anonymous function? Something like this? #({:document %}) What would be the correct syntax?


(#(do {:a %}) 1) but the syntax doesn't buy anything over ((fn [x] {:a x}) 1) in my opinion


Thanks you for confirming my suspicion. Yeah, it does not buy anything. fn it is.

👍 4

#(hash-map :a %) is another option

👍 4

Sorry to cut you off @ramon.rios @dpsutton. Did not notice you were in a conversation.


we should have threaded. that's just kinda the way channels works. no reason to apologize 🙂

👍 4

but nice of you to think that 🙂

Ramon Rios17:12:41

No problem @hindol.adhya 🙂 . We should had opened a thread for that

👍 4
Ramon Rios18:12:24

Is there some way of use a !false in clojure?

Ramon Rios18:12:28

For example. i want to check a value with contains? but actually, i would like to check if this value not exist in the map

Ramon Rios18:12:58

like a not-contains?


See in clojure.core: 1. not 2. if-not 3. when-not 4. complement 5. not=


Also, if you are filtering a sequence, remove is a drop in replacement that does the opposite. Filter preserves the matches, remove drops them.

Enyert Vinas18:12:26

for example you can use this (not (contains? coll key))


I need to check that two digits in a number are the same, but my code looks absolutely convoluted, suggestions?

(defn digits
  (loop [result (list)
         n n]
    (if (pos? n)
      (recur (conj result (rem n 10))
             (quot n 10))
; three if :(defn adjacent?
  (:value (reduce (fn [status current]
                    (if (true? (:value status))
                      (if (= -1 (:last status))
                        {:value false :last current}
                        (if (= current (:last status))
                          {:value true :last current}
                          {:value false :last current}))))
                  {:value false :last -1}
                  (digits n))))

Enyert Vinas22:12:13

Do you have any type of restriction on using core functions for this solution?

Lennart Buit22:12:46

Take a peek at partition ( You can use it to ‘split’ your sequence into subsequences of a given length/offset:

(partition 2 1 (range 10))
=> ((0 1) (1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7 8) (8 9))

Lennart Buit22:12:44

(assuming you need to return true if two adjacent numbers are equal)

✔️ 4

you are using :value to propagate the true value, you can just use (reduced true) instead - that causes reduce to stop reducing and return with that value


that way you don't need to use a hash-map as the reduction - you can either return nil (or false), vs. reduced true to exit with that value


(ins)user=> (reduce (fn [_ v] (println "checking" v) (when (= v :magic) (reduced true))) nil [:a :b :c])
checking :a
checking :b
checking :c
(ins)user=> (reduce (fn [_ v] (println "checking" v) (when (= v :magic) (reduced true))) nil [:a :magic :b :c])
checking :a
checking :magic


but agreed, the partition solution is elegant - reduced is good to know about when you need both accumulated state over a collection, and early return


another example, actually doing your task

(ins)user=> (reduce (fn [prev v] (println "checking" prev v) (if (= prev v) (reduced true) v)) [:a :a :b :c])
checking :a :a
(cmd)user=> (reduce (fn [prev v] (println "checking" prev v) (if (= prev v) (reduced true) v)) [:a :b :c])
checking :a :b
checking :b :c
(ins)user=> (true? (reduce (fn [prev v] (println "checking" prev v) (if (= prev v) (reduced true) v)) [:a :b :c]))
checking :a :b
checking :b :c

👍 4

wait, f from reduce remembers previous element?


nice solution partition!


the f from reduce gets the accumulator, which is the return value of the previous step, so you can make it remember the previous element by returning it


oh ok it's the accumulator


I didn't know about reduced


I would simply use partition like above and run it through distinct and then look for a set with only 1 distinct element. partition, distinct etc. are lazy, so no extra work is done.


You could use partition to get adjacent pairs, filter the ones that are equal and return the second one of the first thing, like this:

(defn adjacent? [n]
  (->> (digits n)
       (partition 2 1)
       (filter (fn [[x y]] (= x y)))
       (some second)))
By using partition-by though, you don't need filtering.
(defn adjacent? [n]
  (->> (digits n)
       (partition-by identity)
       (some second)))
Both of the above behave exactly as your fn, returning the repeated digit, with the one difference being, they return nil instead of false when there aren't any repeated digits. But in Clojure, both false and nil are falsey, enabling the use of this fn as a predicate.