This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-05-29
Channels
- # announcements (1)
- # babashka (83)
- # beginners (67)
- # chlorine-clover (22)
- # cider (11)
- # circleci (6)
- # clj-kondo (12)
- # cljs-dev (137)
- # cljsrn (15)
- # clojure (124)
- # clojure-europe (40)
- # clojure-italy (1)
- # clojure-nl (3)
- # clojure-norway (1)
- # clojure-serbia (3)
- # clojure-spec (19)
- # clojure-uk (14)
- # clojuredesign-podcast (5)
- # clojurescript (80)
- # conjure (49)
- # core-async (62)
- # cursive (18)
- # datascript (1)
- # datomic (64)
- # docker (28)
- # emacs (20)
- # figwheel-main (249)
- # fulcro (95)
- # graalvm (2)
- # jobs-discuss (11)
- # joker (2)
- # juxt (4)
- # lambdaisland (9)
- # leiningen (1)
- # meander (14)
- # mount (6)
- # off-topic (16)
- # pathom (46)
- # re-frame (35)
- # reagent (6)
- # reitit (5)
- # shadow-cljs (28)
- # spacemacs (6)
- # sql (18)
- # tools-deps (26)
- # vim (8)
- # xtdb (23)
- # yada (1)
I'm working on a re-frame app, and I find myself sprinkling the shape of the app-db all over my reg-sub and reg-event-fx calls. It feels like a smell to me, but I'm not sure if that's just idiomatic for clojure
for instance, I have some reg-sub
that uses (get-in db [:foo :bar :baz])
. Should I be reusing an rf/subscribe
call there?
If I recall correctly subscriptions set up state, so it might make things faster. But I wouldn't see it as necessarily "cleaner" from a readability standpoint. Might wanna ask in #re-frame
It makes things more difficult in the REPL, but I also don't know if that should be a guiding principle either.
Reusing the subscriptions has the advantage that it will be shared between all the "users" of that (components or other subscriptions), but I agree that it may introduce unneeded ceremony if you're not sharing anything. But who knows what will be shared tomorrow...
If the query target is really shared I think it's safer to capture that, subscription or other wise. The link is visable and updaters should see how the check how query is called. Without the link, no one can. I'm not thinking in reframe though, which is going to be the real arbitrar here.
For style guide, is this what people use or something else? https://guide.clojure.style/
That's probably the most widely referenced style guide. I'd suggest looking at some of the linting tools: clj-kondo
in particular, but also Eastwood and maybe Kibit.
It's good to read the discussions in the Issues as well, for the style guide @danielostling https://github.com/bbatsov/clojure-style-guide/issues
Okay, thanks! :)
Using a recursive spec, the presens of s/and
seems to affect the result even though there is just one predicate. I would expect (s/and pred)
to be equivalent to pred
. Am I missing something?
(s/def ::list (s/+ (s/cat :s symbol? :r (s/? ::list))))
(s/conform ::list '[a [b]]) ;; => :clojure.spec.alpha/invalid
(s/explain-str ::list '[a [b]]) ;; => "([b]) - failed: Extra input in: [1] spec: :user/list\n"
(s/def ::list-and (s/+ (s/cat :s symbol? :r (s/? (s/and ::list-and)))))
(s/conform ::list-and '[a [b]]) ;; => [{:s a, :r [{:s b}]}]
I think this is due to s/?
being a sequence regex and when you wrap s/and
around ::list-and
like that, you're essentially saying "this is a sub-spec" and it doesn't get "unrolled" by s/?
like a plain spec or predicate would.
It would be like wrapping something in s/spec
(which can be used to prevent unrolling of sequence regex forms).
use s/& to prevent that
So (s/? (s/& ::list-and))
i dont follow. s/& yields the same error as the ::list spec
(s/def ::list-amp (s/+ (s/cat :s symbol? :r (s/? (s/& ::list-amp)))))
(s/explain-str ::list-amp '[a [b]])
;; => "([b]) - failed: Extra input in: [1] spec: :user/list-amp\n"
Yes, that's what Alex was saying was equivalent. If you want [b]
to be valid, wrap ::list
in s/spec
(which is what I said would behave like s/and
). It wasn't clear which behavior you wanted...
ok, so to do recursion you need something that “marks” the recursive step as a sub-spec? s/spec
, s/and
etc..
It's not about recursion, it's about whether you want a sequence regex unrolled or not. You haven't said which behavior you want.
Do you want (s/conform ::list '[a [b]]) ;; => :clojure.spec.alpha/invalid
or do you want that to conform/succeed?
I want it to conform. Think I have enough to figure it out now. Thanks very much for the help!
I recently saw an interesting string building "trick" of (->> ["Line 1", "Line 2", "Line 3"] (str/join \newline))
(for multiline strings) I'm curious if there's any other similar common idioms for string templating/building.
I think this is due to s/?
being a sequence regex and when you wrap s/and
around ::list-and
like that, you're essentially saying "this is a sub-spec" and it doesn't get "unrolled" by s/?
like a plain spec or predicate would.
Anyone used Java Lombok annotated class from Clojure? My code does not seem to find the accessors. The class is here: https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/dev/src/main/java/com/microsoft/aad/msal4j/PublicClientApplication.java It inherits from AbstractClientApplicationBase which has the Lombok annotations. The code I am trying to run,
(def client
(-> (PublicClientApplication/builder "<client_id>")
(.authority " ")
.build))
The error I am getting: No matching method authority found taking 1 args for class com.microsoft.aad.msal4j.PublicClientApplication$Builder
Decompiled AbstractClientApplicationBase: https://pastebin.com/xnXHhpN3
Decompiled PublicClientApplicaton: https://pastebin.com/TH4vSKcNprivate String authority;
I wouldn't expect this to be available to any clojure code without reflection hacksoh I see there's also a public authority override lower down - I don't know enough about java to go much further than that
never mind, the AbstractClientApplicationBase is the one that matters here and its authority method is public
clojure doesn't use the java source, it just goes by the bytecode, so anything java can find, clojure should find too
but I admit I'm not a java or lombok expert
I now don't think it is related to Lombok at all. Lombok did it's thing and generated the Java class.. Maybe I am doing something very silly.
it might help to make this work via very simple java code first then translate?
agreed that lombok shouldn't be making a difference here
https://www.github.com/AzureAD/microsoft-authentication-library-for-java/tree/dev/src%2Fsamples%2Fpublic-client%2FDeviceCodeFlow.java I am translating this Java code (line 29).
What can be happening is: https://clojure.atlassian.net/browse/CLJ-1243
Hello, I want to introduce clojure spec to validate API endpoints. The parsed response naturally does not have namespaced keywords. How do I go about validating the data? Should I simply use unqualified keys? Where should I keep my spec?
you can use s/keys with :req-un for unqualified keys
Hey guys, a few days ago I found this tweet https://twitter.com/TimSweeneyEpic/status/1265451572353552384 and I came up with a solution for clojure
(defn printxy [x]
(dotimes [n x] (println
(apply str
(concat
(repeat n "X")
(repeat (- x n) "Y"))))))
I'm just curious what are other approaches to solve this?@lukas.block I believe your solution is incomplete, since I believe the question asks for every permutation
@lukas.block if you use https://github.com/clojure/math.combinatorics it should be easy
(map #(apply str %) (clojure.math.combinatorics/selections [“x” “y”] 4)) => (“xxxx” “xxxy” “xxyx” “xxyy” “xyxx” “xyxy” “xyyx” “xyyy” “yxxx” “yxxy” “yxyx” “yxyy” “yyxx” “yyxy” “yyyx” “yyyy”)
seems like you aren't "testing your programming language's expressiveness" any longer
can't you do this with standard doseq
?
@noisesmith not sure… that variable “n” length is the issue I think
yeah - fiddling with it a bit I think we need nested for
plus distinct
and I don't think clojure can do this idiomaticallywithout self-call recursion either
This is sort of a bit masking problem… wonder if you can use something with bits to do it… that would be from Java
you can use bits in clojure
right you would built a permutation out of a range of N! numbers from 0 up
then use bit-shift-left of 1 recursively to build the "flag checks" that tell you if a given position is "X" or "Y"
clever
bitwise is the easy way to do this for sure
@raspasov the other trick is we need to use bigints for this as the original post specifies not being limited to some maximum input size
(import (java.math BigInteger))
(defn xys
[n]
(let [permutations (.shiftLeft BigInteger/ONE n)
values (->> BigInteger/ZERO
(iterate #(.add % BigInteger/ONE))
(take permutations))]
(doseq [bitfield values
index (range n)]
(print (if (.testBit bitfield index)
'X 'Y))
(when (= index (dec n))
(newline)))))
why not, I made hinted versions of xys and an alternate xys-loop that uses recursion instead of collection ops
(import (java.math BigInteger))
(defn xys
[n]
(let [permutations (.shiftLeft BigInteger/ONE n)
values (->> BigInteger/ZERO
(iterate #(.add ^BigInteger % BigInteger/ONE))
(take permutations))]
(doseq [bitfield values
index (range n)]
(print (if (.testBit ^BigInteger bitfield index)
"X" "Y"))
(when (= index (dec n))
(newline)))))
(defn xys-loop
[n]
(let [target (.shiftLeft BigInteger/ONE n)]
(loop [^BigInteger permutation BigInteger/ZERO]
(when (< permutation target)
(loop [index 0]
(if (= index n)
(newline)
(do (print (if (.testBit permutation index)
"X" "Y"))
(recur (inc index)))))
(recur (.add permutation BigInteger/ONE))))))
the performance is only slightly better without collections
(cmd)user=> (time (binding [*out* (io/writer "/dev/null")] (xys-loop 20)))
"Elapsed time: 10609.954857 msecs"
nil
(cmd)user=> (time (binding [*out* (io/writer "/dev/null")] (xys 20)))
"Elapsed time: 11120.227152 msecs"
nil
(the rebinding of *out*
factors out some of the overhead time in printing, and avoids needing to do impossible scrollback)