This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-04-10
Channels
- # announcements (4)
- # asami (36)
- # babashka (11)
- # beginners (31)
- # calva (71)
- # clojure (51)
- # clojure-boston (2)
- # clojure-europe (3)
- # clojure-nlp (4)
- # clojuredesign-podcast (1)
- # clojurescript (30)
- # community-development (25)
- # css (1)
- # cursive (10)
- # datomic (1)
- # events (1)
- # figwheel-main (1)
- # girouette (5)
- # graalvm (18)
- # graphql (1)
- # honeysql (37)
- # instaparse (3)
- # jobs (1)
- # malli (6)
- # polylith (2)
- # releases (3)
- # remote-jobs (5)
- # rum (1)
- # shadow-cljs (16)
- # sql (1)
- # tools-deps (37)
- # vscode (11)
Cant quite find what the recommended solution to this is on the interwebs:
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
@U01ML6ZL2U8 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)
I wonder if it’s more popular in the UK than the USA?
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? %))
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]
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
(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!think about how you would have to spec fun
if it had two args, and how that would structurally change the :args
spec
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
Oh! I was wondering why it was in parens. I only get it now that I’m thinking about it being in a list.