Fork me on GitHub
#beginners
<
2019-12-02
>
talktoj04:12:00

Hi All, can anyone help me out with, how we can debug clojurescript in visual studio ? Please share steps if possible. Thanks.

Gudivas10:12:08

Hi All Any good courses for learning closure

uosl11:12:10

I've heard good things about https://purelyfunctional.tv/

Gudivas13:12:35

Thank you... but any free resource available

uosl13:12:48

I don't know of any free video courses. This book is free however https://www.braveclojure.com/

Gudivas13:12:35

Thank you... but any free resource available

nmkip13:12:25

Is it okay for a predicate to return truthy/falsy values? Or they should always return true/false?

(defn repeated-first? [[first & rest]]
  (some #{first} rest))

(repeated-first? [1 2 35 6 1])
;; => 1

(repeated-first? [13 2 35 6 1])
;; => nil

dpsutton13:12:01

Always up to you but I think most would be surprised to see the question mark on the end of that and no Boolean.

parens 12
nmkip13:12:53

Yep, I would also be surprised hehe

nmkip13:12:59

I was just checking 😄

avfonarev17:12:52

What is the easiest way to put a vector into a map using index as a key?

avfonarev17:12:30

I can write code using a loop and transient, but there might be something more straightforward…

Emmanuel17:12:24

(into {} (map-indexed (fn [i e] [i e]) [:hey :there :buddy]))
=> {0 :hey, 1 :there, 2 :buddy}

avfonarev17:12:53

Thanks! I believe, you can shorten it to (into {} (map-indexed vector [:hey :there :buddy]))=> {0 :hey, 1 :there, 2 :buddy}

Emmanuel17:12:22

nice! i was trying to think of how to do that haha

Emmanuel17:12:30

#beginners amiright

alexmiller17:12:18

(zipmap (range) [:hey :there :buddy])

👏 8
👍 8
alexmiller17:12:49

zipmap takes a key seq and a val seq and zips them up (and stops at end of first finite seq)

Eric Ihli18:12:58

[:> Dialog {:full-screen true :open @show-login-screen? :on-close #(dispatch [:show-login-screen false])
                   :Transition-component [:> Slide {:direction "up"}]}
What is the :> symbol in that code? Is that something the community has standardized for something, something from Reagent?

manutter5118:12:10

I posted an incorrect answer yesterday-- :> is the syntactic sugar for reagent/adapt-react-class, see under “Creating Reagent Components from React Components” at http://reagent-project.github.io/docs/master/InteropWithReact.html

💯 4
ghadi18:12:05

:> is a keyword

ghadi18:12:20

just like :foo

ghadi18:12:48

it's up to the library that is inspecting that datastructure to decide what it means. In this case, I believe that is something special in Reagent

ghadi18:12:09

not to be pedantic, but foo and > are symbols, whereas :foo and :> are keywords

manutter5118:12:31

This is incorrect-- :> is the syntactic sugar for reagent/adapt-react-class, see under “Creating Reagent Components from React Components” at http://reagent-project.github.io/docs/master/InteropWithReact.html

AJ Jaro18:12:29

Is there somewhere that I can find the documentation about :> related to create-class or is that just common knowledge?

AJ Jaro14:12:02

@UCQL6E7PY On a similar note, do you know where I can find documentation on :<> as well?

enforser16:12:32

Here’s the pull request where they were added! Looks like it’s short hand for a React Fragment https://github.com/reagent-project/reagent/pull/352

enforser16:12:43

Just FYI, to find the first one I searched ‘reagent colon greater than’ on google, and for the 2nd one I went to the Reagent changelog and CTRL-F’d for :<> . Just mentioning my method in case you run into more special keywords that don’t have easy to find documentation @UGMAVSMUM

AJ Jaro16:12:07

@UCQL6E7PY Thanks for the assist! They were certainly difficult ones to find so I appreciate you sharing your search methods

Darrell19:12:49

I’m working on a test case with clojure.test but when I try to run code in my REPL it’s giving me an error because my database spec is empty. I see some setup at the top of the test file that seems to set up mock data. For example: (use-fixtures :once #'fix/global-fixture) with global-fixture being defined as:

(defn global-fixture
  "should be used in every test namespace"
  [f]
  (binding [*test-context* {::db/pool (mk-pool) ::conf/config test-config}]
    (log/with-level :error (f))))
So in my code, to get the database spec I have (::db/pool fixtures/*test-context*). So, my question is, how can I initialize all of those fixtures in the REPL so that I can call my function with a spec that doesn’t eval to nil?

noisesmith19:12:10

how do you run the test code?

Darrell19:12:46

I use vim-fireplace so usually I do cpr to run all the tests or :.RunTests on one of the test to run just that one.

noisesmith19:12:04

if you call the test as if it were a function, you can pass it as an arg to the fixture, otherwise if using clojure.test functions the fixture should be applied already

alexmiller19:12:25

it depends which functions you call

Darrell20:12:43

So, here’s an example test I have:

(deftest checking-something
    (let [id (data.stuff/first-id (::db/pool fixtures/*test-context*))]
        (testing "something"
            (is (= id 99)))))
And I can run that test with cpr to run all the tests and they’ll run just fine. But if put my cursor over (data.stuff/first-id (::db/pool fixtures/*test-context*)) and type cpp to just run that one function in the REPL it fails because (::db/pool fixtures/*test-context*) evals to nil.

Darrell20:12:36

Actually, I’m wrong. cpr also gives me an error. Both basically saying that test-context is nil or null.

noisesmith20:12:49

right, because in both cases you are running a function, not a "test", per se, so the fixture isn't applied

noisesmith20:12:14

see @alexmiller’s link above - there should be something in fireplace that hooks into that as well

noisesmith20:12:32

hmm, cpr should be applying the fixture

Darrell20:12:35

I’m getting Unable to resolve symbol: test-vars in this context when I run (test-vars fix/*test-context*) . Is that the wrong way to run it?

noisesmith20:12:46

it's clojure.test/test-vars

alexmiller20:12:07

I think it takes a collection of vars too iirc

4
Darrell20:12:11

Ah, that fixed that problem at least. 🙂

Darrell20:12:41

So, I should be able to run test-vars (GOD does Slack’s new markdown handling suck) and then run, for example; (::db/pool fix/*test-context*) and get something more than nil?

noisesmith20:12:48

if ::db aliases the same ns in your test ns as it did in the fixture ns

noisesmith20:12:38

perhaps it would help to look at what fix/**test-context** is

Darrell20:12:18

It’s initially defined as (def ^:dynamic *test-context* {}) but then global-fixture seems to set it up:

(defn global-fixture
  "should be used in every test namespace"
  [f]
  (binding [*test-context* {::db/pool (mk-pool) ::conf/config test-config}]
    (log/with-level :error (f))))
Which makes me question whether global-fixture is getting called within my repl. I’d think so since I have (use-fixtures :once #'fix/global-fixture) in my test file.

noisesmith20:12:05

you are doing the things that ensure any fixtures get invoked, which is why I suspected it might be you are using the wrong key

noisesmith20:12:30

you could eg. (print (keys fix/**test-context*))*

noisesmith20:12:08

this new wysiwyg editing mode for inline source is terrible

Darrell20:12:04

That returns nil so yeah, looks like those fixtures aren’t getting set up somehow.

noisesmith20:12:30

what happens if you call the fixture by hand with your test function as an argument?

noisesmith20:12:39

it could be the fixture is broken also

Darrell20:12:53

Except that they are because tests that use, for example, fix/*test-context* that were written by someone else are passing.

noisesmith20:12:37

any chance you are calling something lazy in your test, so that by the time the value is forced the context binding the values has exited?

noisesmith20:12:13

that's not the case in your example, but it's definitely a gotcha with dynamic bindings

Darrell20:12:32

I moved (print (keys fix/*test-context*)) to right after the use-fixtures call and still get nil.

noisesmith20:12:34

that's not the right place to use it - put it inside the test code itself

noisesmith20:12:46

code at the top level will never be run inside the fixture

noisesmith20:12:27

@darrell this is my attempt at a minimal demo of how it works properly

foo$ clj
Clojure 1.10.0
(cmd)user=> (require '[clojure.test :as test :refer [deftest is test-vars use-fixtures]])
nil
(cmd)user=> (def ^:dynamic *foo* nil)
#'user/*foo*
(cmd)user=> (deftest foo-test (is (= *foo* 2)))
#'user/foo-test
(cmd)user=> (test-vars [#'foo-test])

FAIL in (foo-test) (NO_SOURCE_FILE:1)
expected: (= *foo* 2)
  actual: (not (= nil 2))
nil
(cmd)user=> (defn foo-fixture [f] (binding [*foo* 2] (f)))
#'user/foo-fixture
(cmd)user=> (test/use-fixtures :once foo-fixture)
#:clojure.test{:once-fixtures (#object[user$foo_fixture 0x5c00384f "[email protected]"])}
(cmd)user=> (test-vars [#'foo-test])
nil
(ins)user=> ^D

Darrell23:12:28

Thanks @U051SS2EU! I’ll take a look and apologies for the delayed reply. I had to step away for a few hours.

Gulli22:12:57

What are people using to debug code in VS Code?

seancorfield23:12:19

@glfinn83 What do you mean by "debug"?

seancorfield23:12:11

(I get the impression that very few Clojure devs use a "traditional" (i.e., step) debugger -- but instead use a variety of REPL-based techniques)

anonimitoraf23:12:21

off-tangent but, I've found that Spacemacs' CIDER has a step-debugger that just works

seancorfield23:12:16

FWIW, I haven't used a step debugger for maybe two decades now -- and I've never felt the need for it in Clojure.

anonimitoraf23:12:05

yea I've found this to be the case too. It's so much easier to reason about your code and I think thats how it should be. Although, one thing that trips me up sometimes is holding the types in my head (since, dynamic typing). Any tips to overcome that?

seancorfield00:12:41

I don't know. I just don't find that to be a problem. In what sort of situations do you find that to be a problem?

seancorfield23:12:31

(I used to rely on a step debugger when I did C++ and I used them a little when I first got started with Java in the late 90's but not since)

Gulli23:12:41

Exactly what Raf said. Using REPL is nice, but it would also be good to be able to step through running code.

anonimitoraf23:12:12

VScode is actually my main editor but Calva was conflicting with my vim keybindings, so I decided to give Spacemacs a try. Really great so far (for Clojure, at least)

Gulli23:12:51

Thanks, I'll check it out

👍 4
didibus23:12:44

Cider's debugger is pretty sweet. Its not exactly like a traditional debugger, and can't debug into Java code, but I recommend trying it out if you use Cider. Check it out: https://docs.cider.mx/cider/debugging/debugger.html

didibus23:12:01

Or just try doing C-u C-M-x on a function and then calling it.