Fork me on GitHub
#clojure
<
2018-01-12
>
dpsutton01:01:30

There's a 'base_url' in ring if you're using that

qqq01:01:42

does clojure have a tool that does something like this: input: here's a bunch of code here's a bunch of tests output: give me a list of sexps in the code that were NOT executed when all the tests have been run

qqq03:01:49

anyone familiar with how this black magic: https://github.com/klutometis/reader-macros works ?

Alex Miller (Clojure team)03:01:51

hacks the Clojure reader to open up the dispatch tables and simulate lisp reader macros

gonewest81803:01:05

How do I set a breakpoint in cloverage to debug how it fails to instrument a piece of code?

qqq07:01:27

(def _1 20) <-- is this asking for trouble? I'm doing some pattern matching work, so I'm thinking of naming some pattern vars _1 2 3 ...

andy.fingerhut07:01:54

I am not aware of any parts of Clojure that would break because of using names for Vars like that.

andy.fingerhut07:01:31

You might confuse some readers of the code.

andy.fingerhut07:01:28

You are on the right side of the official docs for symbol names with such names: https://clojure.org/reference/reader#_symbols

andy.fingerhut07:01:53

The symbol by itself is by convention_ only used for Vars or locally bound names (e.g. in let/loop binding forms) that will not be used in the body.

andy.fingerhut07:01:40

I mean to start that last sentence with "The symbol '_' by itself", but my Slack special formatting-fu is weak.

qqq07:01:22

Great; thanks, I think I will be okay, as I tend to use 1 2 only "pre macro expansion", in that I have these rewrite rules defined as: (add 1 2) -> (+ (eval context 1) (eval context 2)) then this "rule" gets translated into a sexp with destructuring , let, <-- and symbols in this form are all gensymmed, so there's no actual 1 2 n the 'post macro' final code

justinlee08:01:39

I have an array of maps. I want to update one of the maps by matching on one of its properties. This (for [x arr] (if (== (:id x) 21) (assoc x :name "new") x)) works, but is there a more idiomatic way. Something like a assoc where the match is an arbitrary predicate?

schmee08:01:45

@lee.justin.m Specter is your friend for these things:

user=> (def a [{:name "foo" :id 1} {:name "bar" :id 2}])
#'user/a
user=> (transform [ALL (selected? :id (pred= 2))] #(assoc % :name "not-foo") a)
[{:id 1 :name "foo"} {:id 2 :name "not-foo"}]

itaied08:01:05

I'm trying to learn clojure spec from the offical site, but I don't understand the instrument and check. Does it replace testing? Should I count on random generation for production testing? When should I use it and what functions should I spec?

martinklepsch10:01:22

How can I create a var from a string, e.g. like (var (symbol "bidi.bidi/match-route")) (this doesn’t work)

turkkam10:01:42

(resolve (symbol "clojure.core" "get")) or (resolve (read-string "clojure.core/get"))

turkkam10:01:36

you are welcome 🙂

itaied10:01:40

I'm trying to spec a configuration function. The function input is env vars (map of keyword to string), and return the port as an int. How should I write the s/def for the key port, once for string and once for int?

(def port-regex #"\d+")
(s/def ::port (s/and string? (partial re-matches port-regex)))
(s/def ?port? int?)
(s/def ::config (s/keys :req-un [::port]))
(s/def ::parsed-config (s/keys :req-un [?port?]))

vemv10:01:38

I'd go for: s/def ::port-input s/def ::port-output

andre.stylianos10:01:56

The above suggestion sounds good, but ::port-output can be just ::port as it's the expected format

itaied10:01:55

yea but I want the return map to contain :port key, and the input map already contains :port key

itaied10:01:59

The only way I could think of is to modify the namespace

andre.stylianos10:01:00

I don't have the full picture but what I'd go with is (s/def ::port (s/or :parsed-port ::parsed-port :port-str ::port-str)) - ::parsed-port and ::port-str are both ports, they just differ in representation. - Your function receives a ::port-str and returns a ::parsed-port

vemv10:01:01

anyone using autoformatters or similar tools has recently had as-> receiving a different indentation style? we haven't updated cljfmt or such. dunno where the change comes from

itaied13:01:41

After setting a function spec, where should I validate it? Should I use conform in the beginning and throw in case of an :invalid?

fmind13:01:01

@itaied you can use the clojure.spec.test.alpha namespace https://clojure.org/guides/spec#_testing

josh_tackett13:01:00

Anyone know what the :v :d :a scores mean in the following library? https://github.com/damionjunk/sentimental

;; Compute the EN ANEW for the provided phrase
(anew/score-phrase "I really love my dog." :english)
;; => {:words ["love" "dog"],
;;     :score {:v 8.145000219345093, :d 6.680000066757202, :a 6.1000001430511475}}

schmee14:01:02

@josh_tackett from the paper cited in the readme: valence, arousal and dominance

nathanmarz14:01:07

@schmee @lee.justin.m a cleaner way to do it with specter: (setval [ALL #(= (:id %) 2) :name] "not-foo" a)

Mikko Harju14:01:10

Any ideas how to debug why clojure.tools.namespace/refresh fails with “namespace … not found, compiling:(dev.clj:1:1)“? I’ve got no AOT compilation settings or anything that should intervene with the reload but still it does not work

Mikko Harju14:01:34

The class is loaded properly the first time I enter the REPL

Mikko Harju14:01:48

in the dev.clj file

josh_tackett14:01:24

@schmee Do you know a good clojure sentiment library?

Mikko Harju14:01:31

Ah, it seems that the namespace fails to reload because of something in its contents. I replaced it with just an empty ns declaration and now it refreshed properly

itaied14:01:39

@fmind isn't that for testing? I meant to ask about runtime validations, reject and accept requests according to the spec

vemv14:01:54

I use this approach: - create a little helper that throws/reports errors if something is not valid?. Good place to wrap expound (a library) as well - integrate the helper into :pre / :post / assert

vemv14:01:58

Probably instrument is a more standard setup, but I find it verbose/redundant

fmind15:01:08

Yes, but instrument will only check your function input, not the output and :fn

fmind15:01:28

you can use orchestra to test everything https://github.com/jeaye/orchestra

fmind15:01:18

but it's a trade-off, spec was written so that only the inputs are checked. This is usually the most common source of 'trivial errors'

fmind15:01:29

runtime checks incur have a performance cost

fmind15:01:55

and you can also use the :pre :post assertion built-in defn http://blog.fogus.me/2009/12/21/clojures-pre-and-post/

vemv15:01:04

the trick is to wrap things in assert, which can be optionally elided in clojure(script)

vemv15:01:12

same for pre/post

qqq15:01:01

1. I realize that most functions should be pure. 2. Occasionally, we write functions f, where the idea is that (f 3 g) means that w emake galls (g 0) (g 1) (g 2), in that order, for its side effects. 3. What is a good way to unit f via clojure.test ?

danm15:01:42

g is passed in as an arg to f?

qqq15:01:42

yes, g is passed as an arg to f,

danm15:01:22

We do that by passing in a 'test' implementation of g that does a (swap! expected-g-calls conj params-to-g), where expected-g-calls is (atom [])

danm15:01:47

Then once f has returned we check that expected-g-calls contains the stuff we thought it should, in the order we thought it should

danm15:01:51

We tend to do the functional core/imperative shell split, and anything that's going to require with-redefs to test and can't be refactored for that not to be the case goes in the shell

qqq15:01:08

in particular, let's say we want to unit test a function:

(defn f [n g]
  (doseq [i (range n)] (g i)))

qqq15:01:34

@carr0t: yeah, most of the time, we try to split pure/imperative too; in this particular case, we're building a "distributed object", so we're trying to test things like: when an update is called on this obj, these registered handlers fire off ...

danm15:01:26

boot.user=> (require '[clojure.test :refer :all])
nil
boot.user=> (defn f [n g]
       #_=>   (doseq [i (range n)] (g i)))
#'boot.user/f
boot.user=> (let [g-calls (atom [])
       #_=>       g (fn [i] (swap! g-calls conj i))]
       #_=>   (f 3 g)
       #_=>   (is (= [0 1 2] @g-calls)))
true

qqq15:01:53

1. this is slightly less elegant/verbose compared to previous uses of (ct/is (= ...lhs... ...rhs...)) 2. I don't think one can make it more elegant/concise

justinlee15:01:28

@schmee @nathanmarz Thanks for the tips. specter is the boss and is just what I need.

bj16:01:17

I find exception handling inside of let bindings a bit awkward, and I ran into this try-let project looking for answers. It doesn't really seem like people use it much, so I'm wondering if anyone here has thoughts on alternative ways to address these issues in the motivation section: https://github.com/rufoa/try-let#motivation?

spieden18:01:47

@bj i usually catch within the bindings and bind a value to the symbol that captures the failure and check for it in the body

danielglauser19:01:54

If you were going to add automated acceptance tests to a full stack Clojure(Script) project today, what would you use?

danielglauser19:01:28

I’ve used clj-webdriver before with mixed results. Is there anything else I should look at?

qqq19:01:48

I'd write most of my code as *.cljc, and keep only the DOM portion in cljs

seancorfield22:01:11

We've used clj-webdriver for several years but it's essentially abandonware at this point so I've been wondering about writing new tests in etaion -- can folks give any experience reports about it?

jumar08:01:42

@U04V70XH6 quite limited but I'm happy with it . So far, my main concern was the setup - for chrome quick & easy, the tests are easy to write and work beautifully. However, the tests failed on safari/firefox (I didn't dig into the details yet).

adamfrey15:01:51

We are using it at room key and really enjoyed the ease of which you can use it from the repl. It makes the process of writing tests very quick and interactive. We haven't had any serious problems so far, I submitted a small bug fix or two and Ivan has been quick to respond to our feedback

seancorfield19:01:52

@U06060CK0 That's great feedback, thanks. Knowing it suits REPL-driven test development is very useful, and that the maintainer is responsive. @U06BE1L6T Interesting. We've found clj-webdriver to be really flaky with Chrome but rock solid with Firefox so that would be a bit of a shift for us.

martinklepsch22:01:52

Does anyone know of any whitespace-aware Clojure parsers or approaches to build such?

andy.fingerhut22:01:21

I haven't used it, and I think even the author may anti-recommend it, but sjacket is in that category of tools: https://github.com/cgrand/sjacket I do not know the reasons it may be anti-recommended, if it is.

cgrand23:01:15

sjacket is used for its clojure parser which is based on parsley (which I’m not super happy with). One day I will be fed up and I’ll write a drop in replacement based on instaparse.

seancorfield22:01:11

We've used clj-webdriver for several years but it's essentially abandonware at this point so I've been wondering about writing new tests in etaion -- can folks give any experience reports about it?