Fork me on GitHub
#beginners
<
2019-12-23
>
lackita00:12:47

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?

seancorfield01:12:23

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)

lackita01:12:59

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

Cam11:12:23

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

delaguardo12:12:53

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” 🙂 ) - https://tonsky.me/blog/clojurefmt/

Cam12:12:06

Ok, thanks @U04V4KLKC thats very helpful

littleli15:12:40

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.

Alex Miller (Clojure team)15:12:43

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

littleli15:12:40

🆒 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.

Alex Miller (Clojure team)15:12:36

sounds like a good idea

Alex Miller (Clojure team)15:12:27

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

Ramon Rios16:12:52

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

dpsutton16:12:31

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

dpsutton16:12:10

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

dpsutton17:12:57

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

dpsutton17:12:50

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

dpsutton18:12:53

yes its a macro

dpsutton18:12:00

and a quite popular one in clojure

dpsutton17:12:28

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

dpsutton17:12:51

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

dpsutton17:12:40

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?

dpsutton17:12:45

try those two different forms i put above in a repl

hindol17:12:07

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

dpsutton17:12:52

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

hindol17:12:45

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

👍 4
seancorfield18:12:10

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

👍 4
hindol17:12:05

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

dpsutton17:12:26

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

👍 4
dpsutton17:12:43

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?

jaihindhreddy19:12:18

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

hindol20:12:30

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))

Emanuele22:12:42

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

(defn digits
  [n]
  (loop [result (list)
         n n]
    (if (pos? n)
      (recur (conj result (rem n 10))
             (quot n 10))
      result)))
; three if :(defn adjacent?
  [n]
  (:value (reduce (fn [status current]
                    (if (true? (:value status))
                      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 (https://clojuredocs.org/clojure.core/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
noisesmith22:12:59

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

noisesmith22:12:33

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

noisesmith22:12:01

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

noisesmith22:12:46

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

noisesmith22:12:37

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
true
(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
: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
false

👍 4
Emanuele00:12:01

wait, f from reduce remembers previous element?

Emanuele00:12:30

nice solution partition!

noisesmith00:12:10

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

Emanuele00:12:21

oh ok it's the accumulator

Emanuele00:12:50

I didn't know about reduced

hindol03:12:47

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.

jaihindhreddy07:12:48

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.