clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
2020-12-29T06:49:53.042200Z

I don't think you are supposed to put anomalies inside an ex-info

2020-12-29T06:50:14.042400Z

You're supposed to return them instead. so you would not throw an anomaly, just return one

2020-12-29T06:51:12.042800Z

What happened to clojure.parallel ?

Yehonathan Sharvit 2020-12-29T10:04:54.044100Z

In the history of Clojure paper, I read > In addition to the map read/print support, Clojure has a large functional standard library ford ealing with maps, with functions for creating maps (sorted and hashed), associating and dissociating keys with values, merging, lookup, key selection and enumeration, nested map selection and update, sequencing (a seq of a map yields a sequence of key-value tuples), as well as a library implementing relational algebra on sets of maps. Of course, any map library could have these things, but no language at the time had them as pure functions of high-performance persistent HAMTs. In addition, a large subset of the map functions also work on vectors, which are, of course, also associative. What is the library implementing relational algebra on sets of maps that Rich refers to ?

Sergio 2020-12-30T11:47:41.093Z

yes, the fact that the relation algebra it’s more powerful than we think at the beginning, quoting Eric Normand: I don’t use these nearly as often as I should, even though I know they’re there https://purelyfunctional.tv/mini-guide/clojure-set/

Sergio 2020-12-29T10:11:35.044300Z

from the clojure cheatsheet I think is:

Rel algebra	(clojure.set/) join select project union difference intersection index rename
https://clojure.org/api/cheatsheet

Yehonathan Sharvit 2020-12-29T10:16:54.044500Z

Nice! That’s exactly what I was looking for.

Yehonathan Sharvit 2020-12-29T10:17:17.044700Z

Never noticed that clojure.set provided a bit or relational algebra

Sergio 2020-12-29T10:34:30.044900Z

yes, something that can come handy more often from now on

zackteo 2020-12-29T11:37:31.051600Z

Hi guys, does the compile step in clojure ensure that code within an when block is able to execute, even if that path is impossible? For some strange reason, my tests work fine locally but not when it goes through the github CI. https://github.com/zero-one-group/fxl/pull/16 I added a when block so that a block of code skips some tests if resources/credentials does not exist (because the main repo does not have the secret updated yet

2020-12-30T10:01:08.085800Z

When Clojure loads a namespace, it will evaluate all top level forms. Compilation happens at load, and so will also evaluate all top-level forms. And if you perform AOT, it will in turn load everything to compile it ahead of time, and will also thus evaluate top level forms.

2020-12-30T10:01:27.086Z

What you can do instead is use delay

zackteo 2020-12-30T10:15:10.086200Z

And this happens with lein cloverage?

zackteo 2020-12-30T13:03:38.100900Z

@didibus But where do i force/deref it such that that part doesn't become a top level form either?

2020-12-30T18:26:25.106400Z

Yup, it would happen with cloverage as well, when it loads the namespaces all top level will be evaluated

2020-12-30T18:29:06.106600Z

Something like: (defonce service (delay (gs/sheets-service google-props)))

2020-12-30T18:30:20.106800Z

Or: (defonce service (delay (when-not (empty? (slurp "..."))) (gs/sheets-service google-props)))

2020-12-30T18:31:05.107Z

It does mean everything that uses service now needs to deref service before using it

2020-12-30T18:31:56.107200Z

But it also means that the initialization of service will be delayed to the first read of the var, and no longer run at load time

2020-12-29T12:01:32.051800Z

What do you mean under "able to execute"?

zackteo 2020-12-29T12:38:10.052100Z

@delaguardoI keep getting

LOAD FAILURE for zero-one.fxl.google-sheets-test
clojure.lang.Compiler$CompilerException: Syntax error compiling at (zero_one/fxl/google_sheets_test.clj:37:1).
    data: #:clojure.error{:phase :compile-syntax-check,
                          :line 37,
                          :column 1,
                          :source "zero_one/fxl/google_sheets_test.clj"}
     java.lang.IllegalArgumentException: no JSON input found

teodorlu 2020-12-29T13:26:30.053200Z

what's an idiomatic name for the constant pi/3? For 2 pi?

zackteo 2020-12-29T13:28:41.053900Z

I guess tau is technically 2 pi 😅

✔️ 2
😄 1
2020-12-29T13:29:53.054400Z

you are checking if file exists and in the same PR you added CI step to create such file

2020-12-29T13:30:18.054700Z

.github/workflows/continuous-integration.yml in that file

zackteo 2020-12-29T13:30:33.054900Z

Okay ... so hear me out, I got the exact same error from the previous commit as well

teodorlu 2020-12-29T13:30:43.055100Z

Noted! How would you name pi/4?

zackteo 2020-12-29T13:30:54.055300Z

Cpp seems to do this https://www.quantstart.com/articles/Mathematical-Constants-in-C/

👀 1
🙏 1
zackteo 2020-12-29T13:31:37.055800Z

And I was using this as the check (when-not (empty? (slurp "resources/credentials.json"))

2020-12-29T13:32:38.056Z

> Okay ... so hear me out A bit rude ) I checked commit history, you added this CI step in the first commit in that branch

zackteo 2020-12-29T13:32:56.056200Z

Sorry I didn't mean to come across as being rude

teodorlu 2020-12-29T13:33:57.056400Z

from https://clojurians.slack.com/archives/C03S1KBA2/p1609248654055300?thread_ts=1609248390.053200&cid=C03S1KBA22 pi is not defined. • pi/2 is M_PI_22/pi is M_2_PI. So, "_ as implicit /"

zackteo 2020-12-29T13:34:43.056700Z

Yeap I did add the CI step. So I guess the point is that I have that when because I can't add the secret needed in the main repo yet

2020-12-29T13:34:46.056900Z

from exception I can say only that your code is trying to read the file but found not a JSON and as far as I see you have a check for file presence I think you can add a bit more sophisticated check for condition in when to check if file present and valid

zackteo 2020-12-29T13:35:40.057300Z

So you are suggesting I might want to combine both checks? 😮

zackteo 2020-12-29T13:37:28.057500Z

And apologies again - I was just using > Okay ... so hear me out as a figure of speech. Really do appreciate the help

2020-12-29T13:37:29.057700Z

yes, but I can see why this is might be an overkill ) I could also suggest to simply comment out that test until you configure the secret

zackteo 2020-12-29T13:38:34.057900Z

I see I see! Yeahhh, i think that makes the most sense

zackteo 2020-12-29T13:39:34.058100Z

i also wanted to ask about Clojure's compilation step

2020-12-29T13:40:01.058300Z

about when?

zackteo 2020-12-29T13:40:13.058500Z

I recall that it won't compile unused code cause of that's characteristic of the underlying jvm

zackteo 2020-12-29T13:41:04.058700Z

but I suppose in this case, it might be because the when check depends on an external factor that so I must run the check for it?

zackteo 2020-12-29T13:43:05.058900Z

Okay maybe that's perhaps more of google closure and unused vars if anything :thinking_face:

zackteo 2020-12-29T13:43:40.059100Z

I think I'll just leave as this for now. Thanks again 🙂

2020-12-29T13:44:56.059300Z

when is a macro which expand to (if test (do body)) and if is a special form defined in clojure compiler with a promise to execute only condition and based on result execute one or another branch

zackteo 2020-12-29T13:46:57.059500Z

I see I see

2020-12-29T15:38:52.061800Z

Clojure JIRA is constantly reloading for me. Is someone able to get the page working? https://clojure.atlassian.net/jira/software/c/projects/CLJ/issues

2020-12-29T15:42:39.062400Z

same, but this link https://clojure.atlassian.net/jira/software/c/projects/CLJ/issues/?filter=allissues is working

2020-12-29T15:45:22.062600Z

@delaguardo Thanks!

alexmiller 2020-12-29T15:52:33.063200Z

This is a jira bug that has been reported to atlassian

alexmiller 2020-12-29T15:52:56.063900Z

If you click on a jira issue number rather than a list item that may help

2021-01-04T09:38:09.301200Z

Hi Alex, thanks for your invitation. Should I file my issue or have you done that already?

alexmiller 2021-01-04T14:31:55.308300Z

no, I have not

2021-01-04T17:45:49.315800Z

I opened https://clojure.atlassian.net/browse/CLJ-2598

2020-12-29T16:27:10.064300Z

Thanks. I have requested access to Clojure JIRA from my Atlassian account. I hope this is the right way. I found no other way to log in.

alexmiller 2020-12-29T16:28:43.065300Z

https://clojure.org/dev/dev outlines the contributor process. Note that we typically reserve jira logins for those providing patches

2020-12-29T16:31:03.065500Z

I already contributed to Clojure and I have a small patch for spec2.

alexmiller 2020-12-29T16:33:08.066100Z

Ok, thanks. What’s the spec 2 issue?

alexmiller 2020-12-29T16:33:57.067500Z

FYI, core team is all on vacation this week. I will check in at some point but might not be today

2020-12-29T16:37:32.067700Z

No problem. I wish you a happy new year and will come back to you next year.

Sam Ritchie 2020-12-29T16:54:12.068700Z

Hey all! I”m mostly done with a fairly advanced forward-mode automatic differentiation implementation in Clojure - this was fairly subtle, but the implementation is shaping up to be nice and usable outside of my initial goal / library

Sam Ritchie 2020-12-29T16:56:20.070900Z

basically this lets you augment a bunch of operations like +, -, *, /, log, exp, etc, and turn them into functions you can “differentiate” through - ie, given some f that uses the new augmented fns, (D f) will return a NEW function that, given an input, returns a measure of how sensitive the result is to that input

Sam Ritchie 2020-12-29T16:57:16.071700Z

I wrote it in literate-programming style, so wanted to poll here and see if there was anyone interested in watching / attending a live tour of how to build something like this

Sam Ritchie 2020-12-29T16:57:39.072300Z

here’s current status for anyone curious, working great but not quite tidied. Cheers and hope everyone’s doing well over the holidays! https://github.com/sicmutils/sicmutils/blob/81509823023a39553f2f3a5eab7571ab0b986cd6/src/sicmutils/differential.cljc

verma 2020-12-29T16:59:30.072500Z

➕ 2
1️⃣ 2
🎉 4
athomasoriginal 2020-12-29T17:41:03.078700Z

REPL friendly code question. I have the following code:

(defn app
  []
  (create-ring-handler))

(defmethod ig/init-key :system/http-server [_ {:keys [db-spec opts]}]
  (jetty/run-jetty #'app opts))
The above works nicely. I can re-eval all code defined in app (for example, all the handlers) and jetty picks everything up. I would like to pass around a db-spec explicitly now like this:
(defn app
  [db] <--- note db
  (create-ring-handler db))

(defmethod ig/init-key :system/http-server [_ {:keys [db-spec opts]}]
  (jetty/run-jetty #'app opts)) <-- need to pass db-spec to #'app
is it possible to pass runtime params, like db-spec, to the var quoted app in the multimethod? Or maybe i need to restructure more of my code :thinking_face:

euccastro 2021-01-01T09:51:24.223200Z

> You could wrap the middleware call: (jetty/run-jetty (fn [req] ((wrap-config #'app config) req)) opts) -- I think that would work I have been trying that with no problem until I started using CSRF protection (i.e. ring-anti-forgery), which needs to maintain state across requests

euccastro 2021-01-01T09:54:23.223500Z

you could disable CSRF protection while in development (and I wouldn't rebuild the whole handler on every request in production anyway)

euccastro 2021-01-01T09:54:52.223700Z

esp. when using reitit, which IIUC does some sort of "precompilation" of the routes for speed

euccastro 2021-01-01T10:16:22.223900Z

another workaround would be to keep a ring session store across handlers (e.g., pass that as part of the config)

verma 2020-12-29T17:50:56.079100Z

🎉

17
🎉 3
seancorfield 2020-12-29T17:58:08.079600Z

I would not have expected that first example to work: run-jetty expects a Ring handler function and handlers should always accept the Ring request hash map as their single argument @tkjone

seancorfield 2020-12-29T18:01:19.079800Z

The usual approach in cases like this is to use middleware to wrap #'app and that middleware accepts the various system information and adds it into the Ring request hash map prior to calling the handler:

(defn wrap-config [h config] (fn [req] (h (assoc req ::config config)))

(defmethod ig/init-key :system/http-server [_ {:keys [opts] :as config}]
  (jetty/run-jetty (wrap-config #'app config) opts))
something like that, maybe? (edited to add config as an arg to wrap-config)

athomasoriginal 2020-12-29T18:02:57.080Z

ooops, yeah, you are correct about the first snippet…I oversimplified my actual code sample too much 😞

seancorfield 2020-12-29T18:04:59.080300Z

Note that in the above, once you have started the server, you can't change that middleware without a restart but you can still change the handler.

👍 1
seancorfield 2020-12-29T18:06:38.080600Z

You could wrap the middleware call: (jetty/run-jetty (fn [req] ((wrap-config #'app config) req)) opts) -- I think that would work.

athomasoriginal 2020-12-29T18:07:54.080900Z

Interesting. I will attempt both of those. Good to know.

athomasoriginal 2020-12-29T18:10:58.081100Z

I was thinking in a different way where I was trying to just pass in config to reitit and attach to their :data hash map and that’s what threw me off

athomasoriginal 2020-12-29T18:12:18.081500Z

As always, thank you Sean!!

seancorfield 2020-12-29T18:19:09.081700Z

Ah, I've never used reitit so I don't know if it has anything to make that sort of handler wrapping easier (nor whether it is REPL-friendly).

athomasoriginal 2020-12-29T18:23:46.082Z

No, not specifically from what I can tell, so your approach is still valid.

euccastro 2020-12-29T22:48:26.084300Z

the integrant README has an example setup that would let you reload the middleware too (but you'd have to explicitly suspend and resume; just reloading the handler wouldn't do): https://github.com/weavejester/integrant#suspending-and-resuming

👍 1