This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-12-23
Channels
- # announcements (6)
- # aws (4)
- # babashka (66)
- # beginners (70)
- # calva (2)
- # cider (62)
- # clara (1)
- # clojars (5)
- # clojure (101)
- # clojure-dev (67)
- # clojure-europe (1)
- # clojure-nl (3)
- # clojure-uk (13)
- # core-async (15)
- # datomic (11)
- # defnpodcast (10)
- # duct (22)
- # emacs (6)
- # events (1)
- # fulcro (34)
- # kaocha (3)
- # off-topic (20)
- # pathom (1)
- # re-frame (8)
- # reagent (14)
- # remote-jobs (1)
- # shadow-cljs (58)
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” 🙂 ) - https://tonsky.me/blog/clojurefmt/
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
(.someAction builder "foo" (into-array Operation []))
- like that
Is there a way for execute two side effects on a function?
not sure what you're looking for but you can use a do
expression. And defn's have an implicit do
body
I would like to have to call two assoc-in in a function
To set 2 different values
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 versionThis whay?
It worked
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.
This is a macro, right?
I saw it a lot on java or other functional stuff, but the concept of this and ->>
doens't fit on my mind
(defn check-correspondence [db]
(assoc-in db [::current :policy-correspondence] :prod-manager)
(assoc-in db [::current :invoicing-correspondence] :prod-manager))
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"
OO thinking
I think now it's main main challange, to refactor my way of think for functional and immutable data
And how should be the right way for do it?
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
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 🙂
Is there some way of use a !false in clojure?
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
like a not-contains?
See in clojure.core
:
1. not
2. if-not
3. when-not
4. complement
5. not=
Thank t9u
Also, if you are filtering a sequence, remove is a drop in replacement that does the opposite. Filter preserves the matches, remove drops them.
@ramon.rios there is a not function https://clojuredocs.org/clojure.core/not
for example you can use this (not (contains? coll key))
Thanks
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))))
Do you have any type of restriction on using core functions for this solution?
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))
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
nil
(ins)user=> (reduce (fn [_ v] (println "checking" v) (when (= v :magic) (reduced true))) nil [:a :magic :b :c])
checking :a
checking :magic
true
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
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
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
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.