beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
Michael Lan 2021-04-10T02:43:37.333700Z

Cant quite find what the recommended solution to this is on the interwebs:

dharrigan 2021-04-10T07:08:23.335700Z

I use sut too šŸ™‚

seancorfield 2021-04-10T07:21:13.335900Z

I wonder if itā€™s more popular in the UK than the USA?

dharrigan 2021-04-10T07:21:52.336100Z

Perhaps we can ask to put it on the next State of Clojure survey šŸ˜‰

solf 2021-04-10T02:45:14.333800Z

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

Michael Lan 2021-04-10T02:45:53.334Z

Oh, so that message is basically telling me doing :refer :all is a bad practice šŸ˜…

solf 2021-04-10T02:46:04.334200Z

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

šŸ‘ 2
solf 2021-04-10T02:46:05.334400Z

Yep

seancorfield 2021-04-10T02:58:19.334800Z

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

seancorfield 2021-04-10T03:00:22.335Z

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 (`<!`, &gt;!, &lt;!!, &gt;!!).

seancorfield 2021-04-10T03:01:56.335200Z

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

āž• 1
Richie 2021-04-10T14:50:17.339800Z

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

Richie 2021-04-10T14:50:36.340Z

Why doesnā€™t this work?

Richie 2021-04-10T14:53:22.340800Z

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]

Richie 2021-04-10T14:54:08.341100Z

I guess theyā€™re two distinct questions.

alexmiller 2021-04-10T15:00:27.341600Z

It conforms (outputs) a map

alexmiller 2021-04-10T15:00:51.342Z

cat always matches a sequential coll

alexmiller 2021-04-10T15:01:54.343400Z

regex ops combine to match a single sequential coll. If you want to match nested colls, wrap the inner one in s/spec

Richie 2021-04-10T15:09:43.344600Z

Ah right! Sorry. Thereā€™s enough new stuff that Iā€™m already forgetting things. Thanks!

Richie 2021-04-10T19:07:15.347400Z

(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!

dpsutton 2021-04-10T19:09:57.347900Z

check out (doc s/fdef). The error is how you are specifying the :args spec

dpsutton 2021-04-10T19:10:22.348400Z

think about how you would have to spec fun if it had two args, and how that would structurally change the :args spec

dpsutton 2021-04-10T19:11:21.348900Z

i'll post the "solution" in a thread but it's worth thinking for a second

dpsutton 2021-04-10T19:11:31.349Z

(s/fdef fun
  :args (s/cat :i int?))

dpsutton 2021-04-10T19:12:33.349400Z

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

Richie 2021-04-10T19:15:33.350300Z

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.

šŸ‘ 1
dpsutton 2021-04-10T19:15:42.350400Z

also, notice the error message doesn't say 8 failed: int? but (8) failed: int?.

Richie 2021-04-10T19:17:59.350700Z

Oh! I was wondering why it was in parens. I only get it now that Iā€™m thinking about it being in a list.

Richie 2021-04-10T19:18:27.350900Z

That was really bothering me, thank you so much for pointing that out.

piyer 2021-04-10T20:41:48.352300Z

I am trying to find a blog post on core.async anti-pattern, any recommendations?