beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
hswick 2021-02-11T03:21:34.430300Z

I'm using deps.edn, trying to require a library into the repl and it results in Execution error (FileNotFoundException) at user/eval3 (REPL:1).

hswick 2021-02-11T03:22:16.431100Z

Tried with a few different libraries and the same thing, running on linux. Using different clojure versions doesnt make a difference. Am I missing something?

alexmiller 2021-02-11T03:23:25.431800Z

Can you post your deps.edn and what you’re doing in the repl

hswick 2021-02-11T03:24:28.432100Z

{:deps {selmer {:mvn/version "1.12.33"}}}

hswick 2021-02-11T03:24:53.432500Z

(require '[selmer.parse :as sp])

dpsutton 2021-02-11T03:35:11.432800Z

i think you want selmer.parser with a trailing r

hswick 2021-02-11T03:41:20.433500Z

oof i wasnt certainly missing something, thank you for reaffirming my sanity

dpsutton 2021-02-11T03:43:13.433900Z

no worries. i went to the github page and saw what namespaces were there. and saw the easy to make typo

Rowan Barnard 2021-02-11T04:45:53.438400Z

Hi guys, I'm using the clj command line tools and REPL from Powershell commandline in Windows with Clojure 1.10.1 and I've typed (doc clojure.set/intersection) into the REPL and it just returns nil with no documentation. The only thing I've required into the default user namespace is clojure.specs.alpha aliased as s. Any ideas what's going on?

seancorfield 2021-02-11T04:49:11.439600Z

@flyingpython You'll need to require clojure.set first

seancorfield 2021-02-11T04:49:16.439800Z

It isn't loaded by default.

Rowan Barnard 2021-02-11T04:49:30.440100Z

Oh and I've just found out Clojure can't find clojure.set when I tried to run the intersection function: Execution error (ClassNotFoundException) at <http://java.net|java.net>.URLClassLoader/findClass (URLClassLoader.java:471). clojure.set

Rowan Barnard 2021-02-11T04:50:44.441300Z

I'm not sure why it can't find it; I opened clj from a Powershell terminal in a folder I created for practising from the Programming Clojure book

seancorfield 2021-02-11T04:51:22.441600Z

seanc@DESKTOP-30ICA76:~/clojure$ clj
Clojure 1.10.2
user=&gt; (doc clojure.set/intersection)
nil
user=&gt; (clojure.set/intersection #{:a :b} #{:a :c})
Execution error (ClassNotFoundException) at java.net.URLClassLoader/findClass (URLClassLoader.java:435).
clojure.set
user=&gt; (require 'clojure.set)
nil
user=&gt; (doc clojure.set/intersection)
-------------------------
clojure.set/intersection
([s1] [s1 s2] [s1 s2 &amp; sets])
  Return a set that is the intersection of the input sets
nil
user=&gt; (clojure.set/intersection #{:a :b} #{:a :c})
#{:a}
user=&gt;
^ @flyingpython

seancorfield 2021-02-11T04:51:39.442Z

Like I said, you need to require clojure.set first. It is not loaded by default.

Rowan Barnard 2021-02-11T04:52:27.443Z

OK I didn't realise I thought it would find it if you qualified it with the namespace

Rowan Barnard 2021-02-11T04:52:42.443500Z

Thanks Sean πŸ™‚

seancorfield 2021-02-11T04:52:52.443700Z

No. That happens to work for a few of the core namespaces but you should never rely on it.

Rowan Barnard 2021-02-11T04:53:28.444400Z

Ah OK that is what was confusing me probably because was pretty sure that usually worked

seancorfield 2021-02-11T04:54:27.444900Z

Here's what is currently loaded by default in a REPL:

seanc@DESKTOP-30ICA76:~/clojure$ clj
Clojure 1.10.2
user=&gt; (-&gt;&gt; (all-ns) (map str) (sort))
("clojure.core" "clojure.core.protocols" "clojure.core.server" "clojure.core.specs.alpha" "clojure.edn" "clojure.instant" "clojure.java.browse" "<http://clojure.java.io|clojure.java.io>" "clojure.java.javadoc" "clojure.java.shell" "clojure.main" "clojure.pprint" "clojure.repl" "clojure.spec.alpha" "clojure.spec.gen.alpha" "clojure.string" "clojure.uuid" "clojure.walk" "user")
user=&gt;
(but don't rely on that)

Rowan Barnard 2021-02-11T04:55:45.445600Z

That is all the ones that you can use without explicitly loading them?

Rowan Barnard 2021-02-11T04:56:23.446600Z

Oh sorry didn't read your first line

seancorfield 2021-02-11T04:56:28.446800Z

That is the ones that you can coincidently use without requiring them. But don't. It could easily change at any time.

Rowan Barnard 2021-02-11T04:56:41.447100Z

OK thank you very much Sean πŸ™‚

seancorfield 2021-02-11T04:57:44.448Z

It's always better to explicitly require a namespace and give it an alias and use the alias.

seancorfield 2021-02-11T16:29:17.488900Z

Better than using the full namespace name in qualifiers everywhere? Makes the code easier to read. Better than referring a name into your current namespace? Gives a clear indication at the point of use that a name comes from another namespace. Explicitly requiring all the namespaces you use (and giving them aliases) in your ns form? Provides a straightforward checklist of all your dependencies in one place at the top of the file.

pez 2021-02-11T17:02:16.489400Z

Thanks. It was the β€œalways” that intrigued me. I tend to most often alias things, but with some of the clojure ones I don’t. I get confused with aliases like [clojure.string :as str] and [clojure.set :as set], so I require them w/o aliases. In code bases where I am the one making the call, at least. I also refer in some stuff. Not sure what my criteria are, but anyway. πŸ˜ƒ

seancorfield 2021-02-11T17:05:46.489600Z

I will :refer in a handful of symbols that work better as syntax (like &gt;! etc from core.async) and we've adopted a set of standard aliases for many of the namespaces we used (enforced by clj-kondo`). In tests, I tend to refer in all the parts of clojure.test that I use so there's less "distraction" in the test code. Overall though, I still start from the position that every namespace should be required and aliased -- so, yes, there are exceptions (as there are to every "rule") -- but knowing when to break the rules comes with experience.

pez 2021-02-11T17:26:00.490200Z

Shu-ha-ri

Rowan Barnard 2021-02-11T04:59:00.448800Z

OK will remember this πŸ™‚

tatut 2021-02-11T05:33:23.448900Z

yeah, I have made ripley https://github.com/tatut/ripley which tries to be a similar experience to writing reagent but on the backend

pez 2021-02-11T06:38:06.450Z

Why is it better to alias?

Audrius 2021-02-11T10:05:28.455600Z

I wonder is there a predicate opposite to nil? something like not-nil? or something like that...

mbjarland 2021-02-11T10:07:41.457100Z

quick question about core async, assuming I have a coll and want to push the values to a channel, is the idiomatic way (map #(async/&gt;!! ch %) coll)or is there something more concise?

mbjarland 2021-02-12T09:04:17.005800Z

Wow. Never ran across run! (ha!). Thanks! And yes, a better match when we don't care about the return values.

mbjarland 2021-02-11T10:08:31.457800Z

I saw the onto-chan variants, but they all seem to involve concurrency (as in starting threads, go loops) which is not what I wanted here

lassemaatta 2021-02-11T10:08:35.457900Z

some?

πŸ’ͺ 1
βœ… 1
emilaasa 2021-02-11T10:49:03.461500Z

Is there a recommended way to use date/time in clojure on the JVM?

dharrigan 2021-02-11T10:56:29.462Z

Interop πŸ™‚

dharrigan 2021-02-11T10:56:37.462300Z

Esp if you are using Java 8 and above

dharrigan 2021-02-11T10:56:45.462600Z

I should say, that's my perferred way πŸ™‚

2021-02-11T10:56:45.462700Z

https://github.com/dm3/clojure.java-time I can recommend that wrapper

R.A. Porter 2021-02-11T12:11:29.463900Z

I quite like tick - https://github.com/juxt/tick

william 2021-02-11T12:35:02.465400Z

is there a way to have an add-watch only on part of an atom? (Say an atom is a map, and I want to see when the value of a key changes, but I'm not concerned about other changes and would like to not do useless computation)

2021-02-11T12:43:46.465700Z

There is nothing like that built into Clojure that I can see. You can begin a watch function with the quickest code that determines whether it is an "interesting" change to you or not, and return as quickly as you can if it is uninteresting.

2021-02-11T12:44:42.465900Z

e.g. the body of the watch function could be something like (if-not (identical? (:foo old-val) (:foo new-val)). &lt;useful code here&gt;)

2021-02-11T12:45:11.466100Z

Or often you would care more about = than identical?

william 2021-02-11T12:49:51.466300Z

thanks @andy.fingerhut, I'll use this approach!

william 2021-02-11T12:53:59.467300Z

I find myself repeatedly importing the same convenience namespaces (like [com.fulcrologic.guardrails.core :refer [&gt;defn &gt;def =&gt;]]) in each namespace. Is there a way of doing that once and forall?

2021-02-11T13:01:22.468200Z

There might be some IDE hotkey thing that does something like that, but I suspect that most folks probably just copy and paste those from another existing namespace, when creating a new namespace.

Mno 2021-02-11T13:04:41.470300Z

Good ol copy and paste, works every time.

caumond 2021-02-11T13:05:16.470800Z

Yes a snippet is possible in most ide. It is what i done for test namespace as it is often the same code...

william 2021-02-11T13:11:55.471700Z

thanks, I can set a snippet in emacs, it's probably a good idea

alexmiller 2021-02-11T13:18:35.477900Z

Look at the to-chan / onto-chan stuff in the core.async api

Eric Le Goff 2021-02-11T13:19:14.478400Z

What function would you use to transform a vector like

[:a :b :c :d :e]
into a list of the consecutive pairs , like
[(:a :b) (:b :c) (:c :d) (:d :e)]
? While it is simple to apply functions I already know , on my learning curve I am currently struggling with inferring which core function I could use to solve specific use cases...

tvirolai 2021-02-11T13:21:01.479100Z

(vec (partition 2 1 v))

πŸ‘ 1
alexmiller 2021-02-11T13:21:02.479300Z

Here, I think you want onto-chan!!

tvirolai 2021-02-11T13:21:10.479500Z

Where v is the input.

Eric Le Goff 2021-02-11T13:23:40.479700Z

I knew about partition [n coll] but overlooked partition [n step coll], thanks a lot :)

tomd 2021-02-11T14:18:03.484700Z

Hello, I have a question about automatically restarting agents. clojuredocs shows that restart-agent needs to be called in a separate thread, so shows it being called in a future in the :error-handler. Before I stumbled across this, I tried supplying this error-handler: (fn [ag ex] (send ag (constantly foo))) (where foo is the initial state of the agent), which seems to work fine. Is there any reason to prefer calling restart-agent in a future to my approach?

alexmiller 2021-02-11T14:23:14.485100Z

have you looked at just using (set-error-mode! ag :continue) ?

tomd 2021-02-11T14:24:39.486Z

I have, but that results in the agent keeping the state it had before the error

tomd 2021-02-11T14:25:11.486700Z

In my case, I want to throw the towel in and revert the agent to how it was at the beginning, I think

alexmiller 2021-02-11T14:26:53.486900Z

ok, seems fine to me

tomd 2021-02-11T14:27:06.487100Z

thanks πŸ™‚

mbjarland 2021-02-11T15:18:42.487500Z

yeah what I figured as well. Was hoping for a blocking variant but this works. Thanks!

alexmiller 2021-02-11T15:20:19.487700Z

onto-chan!! will block?

alexmiller 2021-02-11T15:22:46.487900Z

oh, I see what you mean

alexmiller 2021-02-11T15:23:07.488100Z

can you get that by taking from the channel returned by onto-chan?

tws 2021-02-11T15:29:12.488300Z

@elegoff760 this fits my style, YMMV, but I found it very useful to read and understand every function here when I was getting started: https://clojure.org/api/cheatsheet

πŸ‘ 1
alexmiller 2021-02-11T15:29:33.488600Z

seems like that's the return of the thread fn and all puts should be done by then

ivar 2021-02-11T18:05:32.493900Z

I'm an ex-Rails dev looking at adding user auth & roles to a clojure webapp. In rails I'd just use Devise and basically be done with it. I've poked around a bit and found https://github.com/funcool/buddy and https://github.com/cemerick/friend, but neither seems fully featured (though buddy is closer, it still has a lot in the TODO section of the readme) Is there a recommended library for Devise-like functionality that I'm missing or is the standard practice to roll your own?

clyfe 2021-02-11T18:33:03.495300Z

Nothing like Devise, to my knowledge. Do manual atop buddy.

2021-02-11T18:33:24.495600Z

i'm not aware of a Devise-like library, but it has been some time since i looked

2021-02-11T18:34:15.496100Z

i prefer buddy over friend

ivar 2021-02-11T18:35:47.497300Z

thanks for the replies @chrisblom & @claudius.nicolae - both of you prefer buddy over friend - could you say why ?

2021-02-11T18:37:11.498100Z

i found buddy easier to use

2021-02-11T18:39:36.498900Z

it has some pre-built middleware that was easy to integrate: https://github.com/funcool/buddy-auth

clyfe 2021-02-11T18:40:45.499700Z

I prefer buddy because I'm cargoculting.

clyfe 2021-02-11T18:42:18.000900Z

But it's also newer (so it had the opportunity to consider what can be improved in friend), and more modular - piece-wise.

clyfe 2021-02-11T18:46:02.001700Z

And Chas having "left clojure" and https://groups.google.com/g/clojure-sec/c/ceMhYPR0G60.

clyfe 2021-02-11T18:48:19.002900Z

Something Devise-like could be implemented as a https://github.com/duct-framework/duct module (with buddy for warden, and ring for rack).

ivar 2021-02-11T18:51:25.003900Z

thanks for the pointers you two.. much appreciated. I need some hammock time to process πŸ˜› :hammock:

4
javahippie 2021-02-11T18:55:32.004300Z

friend is not really maintained anymore, and was forked and updated by clj-commons to fix a CVE in a dependency: https://github.com/clj-commons/friend

mbjarland 2021-02-11T19:04:52.004600Z

Yeah, you can take from the returned channel, but at that point the conciseness vs map is starting to suffer. Anyways, thanks for sorting out the options. Looking at this I think i might stick with map for now.

alexmiller 2021-02-11T19:51:57.004800Z

vs map, run! is probably preferred for this case btw