Cant quite find what the recommended solution to this is on the interwebs:
I use sut
too š
I wonder if itās more popular in the UK than the USA?
Perhaps we can ask to put it on the next State of Clojure survey š
To not use :refer :all
. Instead, use an alias:
[cheshire.core :as :ch]
...
(ch/parse-string ...)
Or manually refer to the functions you use:
[cheshire.core :refer [parse-string]]
Oh, so that message is basically telling me doing :refer :all
is a bad practice š
This is to avoid polluting your namespace by referring to all kind of functions that you never use. Itās the same reason why :use
is not recommended, and to easily know where a function comes from
Yep
@michaellan202 as a good rule of thumb "always use :as
with an alias", and it's common to use the last unique segment in the namespace as the alias -- but sometimes a shorter, obvious mnemonic is more readable (:require [cheshire.core :as json])
is something you'll see a lot. In the same way that the most common alias for clojure.string
is str
.
There are a few things that are okay to :refer
in, because it makes the code more readable than using an alias. For example, with clojure.test
, it's fine to :refer [deftest is testing]
-- and if you look at clojure.core.async
code, you'll see folks will typically alias it as async
but also :refer
in the operators (`<!`, >!
, <!!
, >!!
).
(In tests, I tend to alias the namespace being tested as sut
-- system under test -- but that's not very common in the Clojure world I don't think)
Hey! clojure spec docs say that cat matches a mapā¦ https://clojure.org/guides/spec > Conforms to map with named keys based on theĀ `cat`Ā tags
(s/explain (s/cat :x int? :y int?) {:x 2 :y 1})
{:x 2, :y 1} - failed: (or (nil? %) (sequential? %))
Why doesnāt this work?
The issue that brought me to this was in trying to match a nested vector.
(s/explain (s/cat :a string? :nested (s/cat :b string? :b string?)) ["a" ["b" "c"]])
gives me
["b" "c"] - failed: string? in: [1] at: [:nested :b]
I guess theyāre two distinct questions.
It conforms (outputs) a map
cat always matches a sequential coll
regex ops combine to match a single sequential coll. If you want to match nested colls, wrap the inner one in s/spec
Ah right! Sorry. Thereās enough new stuff that Iām already forgetting things. Thanks!
(require '[clojure.spec.alpha :as s])
(require '[clojure.spec.test.alpha :as stest])
(defn fun [i]
(inc i))
(s/fdef fun
:args int?)
(stest/instrument `fun)
(fun 8)
(8) - failed: int?
Sorry to be back again so soon. Iām pasting this code into a 1.10.2 clojure repl and Iām getting an error so I donāt think itās anything with my environment or my other code.
I donāt expect an error here, what gives? Thanks in advance!check out (doc s/fdef)
. The error is how you are specifying the :args
spec
think about how you would have to spec fun
if it had two args, and how that would structurally change the :args
spec
i'll post the "solution" in a thread but it's worth thinking for a second
(s/fdef fun
:args (s/cat :i int?))
consider if fun
took two arguments. there would be no way to add the second argument's spec to :args
without changing the cardinality from 1 to many. The answer is that the args is always a sequence of specs, and in this case it has only one spec
Iāll need to use s/cat then. Aww man, I totally missed that. Thanks for pointing that out. Yea, itāll help me to remember to do that if I consider how it would scale to more arguments like you said.
also, notice the error message doesn't say 8 failed: int?
but (8) failed: int?
.
Oh! I was wondering why it was in parens. I only get it now that Iām thinking about it being in a list.
That was really bothering me, thank you so much for pointing that out.
I am trying to find a blog post on core.async
anti-pattern, any recommendations?