beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
seancorfield 2021-05-07T00:00:21.282600Z

(if (seq cols) (apply map + cols) <whatever other value you want>)

seancorfield 2021-05-07T00:00:39.283Z

Or throw an exception perhaps if it “shouldn’t happen”.

zackteo 2021-05-07T00:01:18.283500Z

Okay! Thank you! 😄

seancorfield 2021-05-07T00:02:54.284900Z

If you want an exception and you don’t care about getting a lazy seq of sums, you could just (apply mapv + cols) — it will throw an arity error if cols is empty.

zackteo 2021-05-07T00:03:24.285500Z

Also by any chance do you have example projects using clojure.tools.cli ? Am trying to use it but another reference might help

seancorfield 2021-05-07T00:06:16.286700Z

I don’t know what will be more useful than the examples in the README but clj-new still uses it (even though the old -m command-line is no longer documented). See https://github.com/seancorfield/clj-new/blob/develop/src/clj_new/helpers.clj#L264 for the opts definitions and https://github.com/seancorfield/clj-new/blob/develop/src/clj_new/helpers.clj#L381 for the point of use.

seancorfield 2021-05-07T00:07:22.287300Z

Cognitect’s test-runner uses it too, starting at https://github.com/cognitect-labs/test-runner/blob/master/src/cognitect/test_runner.clj#L77

seancorfield 2021-05-07T00:08:41.288100Z

(although that uses :assoc-fn in places where I’d use :update-fn now)

seancorfield 2021-05-07T00:09:30.288600Z

See this example from tools.cli’s README for what I mean about that:

["-f" "--file NAME" "File names to read"
    :multi true ; use :update-fn to combine multiple instance of -f/--file
    :default []
    ;; with :multi true, the :update-fn is passed both the existing parsed
    ;; value(s) and the new parsed value from each option
    :update-fn conj]

marciol 2021-05-07T00:12:36.290800Z

Hi, I'm trying to apply the bean function over java.net.URL but I'm getting this error:

(bean (java.net.URL. "<https://localhost:80>"))
Error printing return value (ConnectException) at java.net.AbstractPlainSocketImpl/doConnect (AbstractPlainSocketImpl.java:399).
Connection refused (Connection refused)
Anyone can say why this is happening?

dpsutton 2021-05-07T00:16:05.291900Z

(bean (<http://java.net|java.net>.URL. "<https://www.google.com>")) shows it has :content on it. If you look at the error thrown by your function you'll see [java.net.URL getContent "URL.java" 1177] in it. And you presumably don't have a webserver running locally on port 80

marciol 2021-05-07T00:16:39.292200Z

hmmmm, thank you @dpsutton

dpsutton 2021-05-07T00:16:56.292600Z

(slurp (.getContent (<http://java.net|java.net>.URL. "<https://www.google.com>"))) is the contents of the resource

marciol 2021-05-07T00:18:35.293500Z

Yes, the bean function walks over every getter and returns a map.

marciol 2021-05-07T00:19:00.294100Z

This is why I'm getting this error. Thank you again @dpsutton

zackteo 2021-05-07T00:22:50.294400Z

I think i'm generally just a bit confused about how to package my project into a binary (?) or rather I need to read up more cause I have been pretty much only been using lein run

zackteo 2021-05-07T00:24:24.296200Z

@seancorfield I think me asking this would help. Is

my-program -vvvp8080 foo --help --invalid-opt
the equivalent of running the following in the repl ?
(-main "-vvvp8080 foo --help --invalid-opt")

zackteo 2021-05-07T00:24:46.296400Z

This is from the quickstart example. I've been particularly confused about how to get that working

seancorfield 2021-05-07T00:26:41.296800Z

They should be separate strings.

seancorfield 2021-05-07T00:27:10.297500Z

(-main "-vvvp8080" "foo" "--help" "--invalid-opt")

seancorfield 2021-05-07T00:27:29.298Z

-main expects to be called with a sequence of zero or more strings

zackteo 2021-05-07T00:28:50.298900Z

Right right :face_palm: i recall a similar confusion I had when working with clojure.java.shell/sh

zackteo 2021-05-07T00:29:12.299100Z

Thank you!

2021-05-07T01:13:42.300400Z

Just describing my dog in Clojure. Not totally sure if I’m doing this right lol. One area is I want to also describe his reaction to the :triggers but not sure what to do there

zackteo 2021-05-07T01:50:52.301500Z

@rob370 seems fine ? Are you perhaps thinking of the triggers being functions?

zackteo 2021-05-07T01:59:03.302700Z

Is there a way to print out a table when you do lein run ? I know there is clojure.pprint/print-table but is there a direct that to cli output?

zackteo 2021-05-07T02:01:32.303400Z

nvm I just realised it is because I was getting a clojure not found error from not requiring the namespace

craftybones 2021-05-07T02:53:43.303500Z

You could use a (partial reduce + 0) which will give you a zero if you have an empty sequence in the coll

zackteo 2021-05-07T03:05:18.305100Z

How do I compile a leiningen project? Like I can do lein run to use my program. And I understand I can do lein uberjar but have a feeling that's not what I want exactly?

2021-05-07T03:15:41.305200Z

What do you want?

zackteo 2021-05-07T03:16:46.306Z

I think I want a simple way to allow someone to package my repo and run it as an executable. I think https://github.com/BrunoBonacci/lein-binplus comes close

zackteo 2021-05-07T03:18:10.306500Z

Oh i think I can just include it in my project.clj hmm

2021-05-07T03:18:22.306600Z

It depends on your audience then, I usually take an uberjar to mean that, but if your audience is less technical then you will need something additional like that lein plugin

zackteo 2021-05-07T03:20:42.306900Z

How do you run an uberjar actually?

solf 2021-05-07T03:23:33.307100Z

java -jar uberjar.jar

2021-05-07T03:25:03.307200Z

That depends too

2021-05-07T03:25:44.307300Z

You can build jar files that launch like that

2021-05-07T03:27:24.307400Z

But java only knows how to run jvm bytecode, so you either have to hand it jvm bytecode (by aot compiling clojure) or bootstrap (run some bytecode that knows how to load clojure)

zackteo 2021-05-07T03:32:27.308600Z

Hmmmm, will have to dig deeper into that but lein bin works well for now java -jar worked too for the standalone.jar that was produced

2021-05-07T10:11:04.309500Z

what is the meaning of :* in Clojure?

2021-05-07T10:17:26.309800Z

it is a normal keyword, nothing special about it

👍 1
Christian 2021-05-07T13:43:55.313Z

I have a map with thousands of entries and want to find a value of an entry that has my search-value in a nested structure. for example this json:

{
    "id": "nord-finance",
    "symbol": "nord",
    "name": "Nord Finance",
    "platforms": {
      "ethereum": "0x6e9730ecffbed43fd876a264c982e254ef05a0de",
      "binance-smart-chain": "0x6e9730ecffbed43fd876a264c982e254ef05a0de"
    }
  },
  {
    "id": "norse-finance",
    "symbol": "nfi",
    "name": "Norse Finance",
    "platforms": {
      "binance-smart-chain": "0x43f001914c7d347d152f296e8539086fe49f8bd6"
    }
  },
  {
    "id": "northern",
    "symbol": "nort",
    "name": "Northern",
    "platforms": {}
  },
  {
    "id": "nosturis",
    "symbol": "ntrs",
    "name": "Nosturis",
    "platforms": {
      "ethereum": "0xeccf15a4b5976a1365baed5297058b4ca42777c0"
    }
  },
I want to search for the value "0x43f001914c7d347d152f296e8539086fe49f8bd6" in all etherum platform entries and have the id where this unique value is matched. Do I have to iterate through the whole thing?

ghadi 2021-05-07T13:47:08.313400Z

yes

ghadi 2021-05-07T13:47:32.314400Z

unless you build an index data structure that allows for faster access

ghadi 2021-05-07T13:47:59.314900Z

aka stick it in a database and let the database do the heavy lifting

Christian 2021-05-07T13:56:19.315900Z

I did this with the list for now:

(get (first (filter #(= (get-in % ["platforms" "binance-smart-chain"])
                 "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82")
             all-coingecko-coins)) "id")
while all-coingecko-coins is the json turned map from chesire. Is this a lot slower than a database, when I do this a lot?

2021-05-07T14:04:29.316100Z

Besides using a database, you can also create a Cloure map in memory with the keys being the strings you want to search for, and the values being the entire original map {"id": ...} (including the "ethereum" key -- there isn't really any reason to remove that)

2021-05-07T14:05:09.316400Z

That would be one concrete example of 'building an index data structure that allows for faster access' that ghadi mentioned.

ghadi 2021-05-07T14:09:40.317200Z

iterating through a list that is in the thousands is not going to be an issue

ghadi 2021-05-07T14:10:17.317400Z

it all depends on the wider use-case. Doing this in a service? cardinality is actually in the millions+? etc.

Christian 2021-05-07T14:11:49.317700Z

I though about scaling this up. This is for the translation of one api-id-value into another. so I could just do it once, save it and be done

Christian 2021-05-07T14:13:43.317900Z

I could transfrom the whole json to "contractAdress" as a key and the id as a value map. Is this what you mean @andy.fingerhut?

ghadi 2021-05-07T14:15:00.318900Z

yes, an index

ghadi 2021-05-07T14:15:07.319300Z

txid -> tx

ghadi 2021-05-07T14:15:21.320Z

a map of ^

2021-05-07T14:15:52.321300Z

You can change the JSON file if you wish, and have that option, but it is straightforward in Clojure to create the in-memory map with whatever key you want, or even multiple maps with the same Val’s but different keys - whatever you want to search on most often

Endre Bakken Stovner 2021-05-07T15:57:19.323800Z

I have the following let in hiccup:

(let [jobinfo @(rf/subscribe [:jobinfo])
       [:div
        [:h4 (.now js/Date)]
        [:pre (with-out-str (pp/pprint jobinfo))]])
The jobinfo updates just fine in re-frame, but the js/Date is not updated when the jobinfo is updated. How do I fix this?

2021-05-07T16:46:31.326600Z

React might be caching the h4, I’d try

(let [jobinfo @(rf/subscribe [:jobinfo])
      now (.now js/Date)]
       [:div
        ^{:key (str "jobinfo-date-str-" now)}
        [:h4 now]
        [:pre (with-out-str (pp/pprint jobinfo))]])
or something similar

Endre Bakken Stovner 2021-05-10T15:02:52.478300Z

Did not work. I remember reading about this in some book about Clojure web programming - will find the answer 🙂

Endre Bakken Stovner 2021-05-10T15:03:00.478500Z

Thanks btw

Edward Ciafardini 2021-05-07T20:13:54.330600Z

I am trying to figure out a way to use [cheshire "5.10.0"] in my REPL. Is there a best way of doing this? Can I add it as a global dependency? I want to be able to do this in the REPL:

(cheshire/parse-string
  (slurp "<https://jsonplaceholder.typicode.com/todos>"))
Any help would be appreciated

2021-05-07T20:14:44.331500Z

Do you know if you are using Leiningen, e.g. you have a file named project.clj in your project? Or perhaps the Clojure CLI tools, e.g. there is a deps.edn file in your project?

Edward Ciafardini 2021-05-07T20:15:42.331800Z

I am using leiningen - should have mentioned that

Edward Ciafardini 2021-05-07T20:17:24.332700Z

I tried using lein repl and it worked. I was using clj to start the repl for no real reason. Thanks!

2021-05-07T20:18:06.333600Z

They use different files to specify the dependencies of your project, but they can also be started even if the file they look for is not present. It is just in that case it will not notice the file of the other tool

2021-05-07T20:18:49.334200Z

You could choose to sometimes use one tool for a project, sometimes the other, but it would be your responsibility to keep their dependencies consistent between those two files in that case.

👍 1
seancorfield 2021-05-07T20:27:17.336Z

clj -Sdeps '{:deps {cheshire/cheshire {:mvn/version "5.10.0"}}}'
if you just want a quick REPL anywhere with Cheshire without worrying about deps.edn

seancorfield 2021-05-07T20:35:09.337100Z

There’s a useful plugin for Leiningen called lein-try that lets you do a similar thing: Add this to the :user profile in your ~/.lein/profiles.clj file:

:user {:plugins [[lein-try "0.4.3"]]}
and then you can do:
(! 1418)-&gt; lein try cheshire/cheshire 5.10.0
...
user=&gt; (require '[cheshire.core])
nil

Edward Ciafardini 2021-05-07T20:36:31.337300Z

all very useful - I'll save these to my terminal commands .txt file 🌝

Edward Ciafardini 2021-05-07T20:45:09.337500Z

clj -Sdeps '{:deps {cheshire/cheshire {:mvn/version "5.10.0"}}}' This command creates a temporary dependency that only lasts until you close the REPL?

2021-05-07T20:49:33.339600Z

Can someone please help explain what is going on here? I have some tests and assertions defined in (deftest) My file has (run-tests) at the end and I'm running the test by doing Load Current File and Dependencies IF I run the tests I see this:

; Evaluating file: interpreter-tests.cljs

Testing test.interpreter-tests

Ran 2 tests containing 16 assertions.

0 failures, 0 errors.
If I make one of them so that it fails I see this:
FAIL in () (interpreter-tests.cljs:90:9)

add adds

expected: (= {:registers {:a 17, :b 7}, :internal-registers {:par 0}} (math-func :add {:registers {:a 5, :b 7}} [:a 9]))

  actual: (not (= {:registers {:a 17, :b 7}, :internal-registers {:par 0}} {:registers {:a 14, :b 7}, :internal-registers {:par 0}}))


Testing test.interpreter-tests


Ran 2 tests containing 16 assertions.

0 failures, 0 errors.
Why does it still say at the end ran 2 tests containing 16 assertions. 0 failures, 0 errors ? Why doesnt it class my failing test as a failure?

seancorfield 2021-05-07T20:55:13.340500Z

You probably want (test-ns *ns*) instead of (run-tests).

seancorfield 2021-05-07T20:56:24.341900Z

The clojure.test API is pretty strange in terms of what each (similar) function does in terms of fixtures and reporting. Many of the public functions are really low-level pieces that the other functions call — and are more useful to 3rd party test runners.

seancorfield 2021-05-07T20:59:14.343900Z

One of the differences here is that (test-ns *ns*) “Internally binds *report-counters* to a ref initialized to *initial-report-counters*. Returns the final, dereferenced state of *report-counters*.” which I don’t believe (run-tests) does.

seancorfield 2021-05-07T20:59:24.344200Z

Does that help @qmstuart?

2021-05-07T21:00:03.344600Z

yes, thaats perfect, thank you

seancorfield 2021-05-07T21:01:06.345400Z

(that said, I don’t see the behavior you’re seeing when I try (run-tests) in a REPL so I may be wrong)

2021-05-07T21:01:35.346600Z

changing it to use (test-ns), I now see it reporting failure count i would expect

seancorfield 2021-05-07T21:01:46.346900Z

OK, good.

emilaasa 2021-05-07T21:02:25.348100Z

Wouldn't (run-tests) probably just map (test-ns) over the namespaces?

seancorfield 2021-05-07T21:02:36.348500Z

Interestingly, run-tests seems to call test-ns… yes @emilaasa

2021-05-07T21:02:52.348900Z

actually no, I'm being dumb It isn't. It doesnt report the counts at all. But it shows the fails.

seancorfield 2021-05-07T21:03:14.349400Z

I’m just looking at the source… so many of these functions call each other with different setups 😐

2021-05-07T21:03:56.350400Z

ok, i restarted my repl and now i see correct counts. So maybe my file wasn't synced correctly or something.

2021-05-07T21:03:59.350600Z

How should I run tests?

emilaasa 2021-05-07T21:04:01.350800Z

There are plenty of earmuffs in that file - still if you reload your buffer I'd expect it to work

2021-05-07T21:04:25.351400Z

my problem is, I add tests and I want to run them. But sometimes I forget to evaluate them in the repl, then i dont see the pass / fails as I would expect

2021-05-07T21:04:48.352600Z

so I put (run-tests) or now (test-ns) at the end and just evaluate whole file

2021-05-07T21:04:56.353Z

but maybe this isnt best way

seancorfield 2021-05-07T21:05:11.353600Z

Yeah, I think I have it backwards — I actually use run-tests when running tests in a namespace, sorry.

emilaasa 2021-05-07T21:05:32.354300Z

What editor are you using? The ones I've used have had some hooks that probably call (run-tests) under the hood

2021-05-07T21:05:44.354700Z

i'm using calva

emilaasa 2021-05-07T21:06:08.355300Z

Hm and you were doing cljs?

2021-05-07T21:06:13.355500Z

yes

emilaasa 2021-05-07T21:06:29.355700Z

emilaasa 2021-05-07T21:06:32.355900Z

Maybe wont work then

2021-05-07T21:07:00.356400Z

Is it more normal to run tests via repl or via terminal / CLI ?

ghadi 2021-05-07T21:07:06.356600Z

REPL

➕ 1
ghadi 2021-05-07T21:07:15.357300Z

everyone who disagrees is wrong

emilaasa 2021-05-07T21:07:18.357700Z

😄

2021-05-07T21:07:20.357900Z

I come from C# where tests would be automatically run when a file changes

seancorfield 2021-05-07T21:07:22.358100Z

I run tests via my editor (which uses the REPL, of course).

2021-05-07T21:07:22.358200Z

I kinda miss that

emilaasa 2021-05-07T21:07:49.359100Z

Ghadi coming out swinging I like it 😛

🔥 1
2021-05-07T21:08:01.359500Z

😄

seancorfield 2021-05-07T21:08:13.360Z

I have hot keys bound to “run this namespace’s tests”, “run the current test”, and “run the tests in the associated test namespace” — used when I’m working in a source file, so I don’t have to switch to the test file or anything.

NoahTheDuke 2021-05-07T21:08:45.360700Z

run tests through the editor/repl as you develop, and then at the end of a "session", run the whole test suite from the command line to verify your work

seancorfield 2021-05-07T21:08:55.361100Z

Yup ☝️:skin-tone-2:

NoahTheDuke 2021-05-07T21:08:57.361200Z

only have to do that last step once or twice a day

seancorfield 2021-05-07T21:09:22.362600Z

(but that all may be harder with cljs?)

seancorfield 2021-05-07T21:09:45.363900Z

Yes.

2021-05-07T21:09:52.364400Z

> “run this namespace’s tests” Calva has this but I think this is what emilaasa pointed out not working for cljs as the repl says this:

; Evaluating file: interpreter.cljs
nil
; Running tests for exfn.interpreter...
; No tests found. 😱, ns: 0, vars: 0
cljs꞉exfn.interpreter꞉&gt; 

emilaasa 2021-05-07T21:09:52.364500Z

I think you had the right idea @qmstuart with evaluating (run-tests). I'd probably have it as an expression in the file and just run it from there - maybe with marks or something if VSCode supports it

seancorfield 2021-05-07T21:10:12.365Z

It will pick up anything from deps.edn if present as well, but it can also be used in a directory without deps.edn.

ghadi 2021-05-07T21:10:16.365400Z

As a beginner, you're going to want to focus on avoiding doing the easy, lazy thing: jumping out of your live environment to run your tests from the cli

💯 1
ghadi 2021-05-07T21:10:24.365700Z

this takes practice

emilaasa 2021-05-07T21:10:27.366Z

Or maybe there's some REPL command abstraction that you can bind a keystroke to

seancorfield 2021-05-07T21:11:16.366500Z

(comment
  (run-tests)
  ...)

👍 1
☝️ 1
seancorfield 2021-05-07T21:11:38.367100Z

So you can eval it when you want to, rather than having it run every time you eval the file.

emilaasa 2021-05-07T21:12:32.367800Z

@qmstuart I'm not a calva / vscode user but: https://calva.io/custom-commands/ might work

👍 1
emilaasa 2021-05-07T21:14:47.368300Z

I think this is key, every time (atleast that I can remember) I've done things the slower more familiar way I've regretted it and it just took longer to learn the right way. 🙂

emilaasa 2021-05-07T21:17:22.369500Z

@seancorfield Maybe it's these guys that make it a bit brittle: https://github.com/clojure/clojure/blob/28efe345d5e995dc152a0286fb0be81443a0d9ac/src/clj/clojure/test.clj#L260

emilaasa 2021-05-07T21:21:53.370100Z

Hm is that even the code btw that you are supposed to run cljs tests with?

2021-05-07T21:22:50.370500Z

you mean I shouldn't be using (deftest) and (run-tests) to write cljs tests?

emilaasa 2021-05-07T21:23:55.371600Z

I have no experience writing cljs at all but I've been surprised before - sec

emilaasa 2021-05-07T21:24:27.371900Z

(ns my-project.tests
  (:require [cljs.test :refer-macros [deftest is testing run-tests]]))

emilaasa 2021-05-07T21:24:33.372100Z

Is that how you require it?

emilaasa 2021-05-07T21:24:51.372600Z

I went and read the source for clojure test code 🙂

emilaasa 2021-05-07T21:25:05.372900Z

Instead of cljs.test

emilaasa 2021-05-07T21:25:47.373200Z

But it looks like the same deal

2021-05-07T21:25:48.373300Z

I have this

(ns test.interpreter-tests
  (:require [cljs.test :refer [deftest is testing run-tests test-ns]]
            [exfn.interpreter :refer [interpret build-symbol-table mov math-func get-parity]]))

2021-05-07T21:26:16.373700Z

yeah, that looks the same

emilaasa 2021-05-07T21:26:37.374200Z

I'm out of my depth - I think you're doing the right thing and it's hopefully some REPL state messing you up before

emilaasa 2021-05-07T21:29:24.374800Z

Seems to be quite a bit more complicated the code in cljs 😄