Fork me on GitHub
#beginners
<
2018-10-10
>
Chase00:10:14

I put a library in my dependencies and used :require to pull the library in. when I call to it inside my source code and evaluate it it works fine. but if i try to evaluate that exact same expression directly in my repl i'm getting a namespace error. do I have to pull in that library in the repl? why would I if it's already in my source and working there?

Chase00:10:05

i'm using emacs w/ cider if that matters.

seancorfield00:10:58

Code needs to be require'd into each namespace that needs to use it. Your REPL starts in the user namespace which won't have anything required in.

Chase00:10:42

ahh ok. but why does it evaluate fine within my source file?

seancorfield00:10:31

Because that namespace does require it.

Chase00:10:08

ahh, but the repl doesn't put me right in that namespace? it puts me in user?

seancorfield00:10:15

Correct. So another option is:

user=> (require 'serpent-talk.talk)
nil
user=> (in-ns 'serpent-talk.talk)
nil
serpent-talk.talk=> (csk/->snake_case "hi world")
...

seancorfield00:10:58

You would still have to require your code's namespace (`serpent-talk.talk`) to load it, and then jump into that namespace -- and now it's just like you are in your source file's code.

Chase00:10:19

ok, ok. or i just switched to the namespace with (ns serpant-talk.talk) and that worked as intended

Chase00:10:29

which is the more idiomatic way to do this stuff?

seancorfield00:10:43

Be careful. That won't work if you haven't already loaded (required) that namespace!

seancorfield00:10:54

You'll create a new empty namespace instead.

Chase00:10:12

ahh yes. ok, i see. yeah I had already had that code saved before starting the repl

Chase00:10:20

this seems like it could get complicated if you for some reason are adding dependencies and working in the same session. what do folks normally do? save and restart the repl completely?

CyberSapiens9700:10:57

Hello people, what name you usually give to the base value of your reduce functions?

seancorfield00:10:21

It depends on what it represents. Each case tends to be different.

seancorfield00:10:50

(if I'm understanding your question correctly)

CyberSapiens9700:10:30

i always try to find a name that suits the function, but it's weird to read

mfikes00:10:48

@chase-lambert If you are just revising some saved code and want to load the changes, you can (require 'my-ns.core :reload)

seancorfield00:10:57

@chase-lambert With Leiningen, if you add a dependency you generally have to restart the REPL. With Boot, you can add dependencies to REPL interactively. But dependencies are not namespaces, they're external libraries.

seancorfield00:10:46

As @mfikes says, if you're just adding new namespaces, you can just require them, and require .. :reload other namespaces, as needed to (re)load your own code into the REPL.

Chase00:10:36

thanks folks. not sure if this is related but now i'm trying to run this test and getting No test namespace: serpent-talk.talk-test

mfikes01:10:13

@chase-lambert Are you trying to do (clojure.test/run-tests 'serpent-talk.talk-test)? If so, have you required that namespace first?

Chase01:10:20

to be honest, i'm winging it. the tutorial said to use lein test or follow your editor's instructions and i'm trying to do the latter so I learn cider.

Chase01:10:38

So I think i just told cider to run the test while I was in that test file

Chase01:10:19

lein test does work btw. I think I'm just going to have to learn more about the namespaces

dpsutton01:10:23

Cider has a short cut 'C-c C-t p'. (read control c , control t, p) that will run all tests in your project

dpsutton01:10:48

N instead of p for tests in the current namespace and t for the test at point

Chase01:10:51

ok. i'm starting to figure it out. in the repl if i switch to the test file namespace i can run that run-tests command and it works.

Chase01:10:12

when i just tried that shortcut of yours I get a different error so that's progress! haha

Chase01:10:45

Says No assertions were run. Did you forget to use 'is' in your tests? but I did use 'is'

Chase01:10:36

Maybe that means it wasn't reading my test file but just the main file I was in at that time which had no tests in it?

Chase01:10:46

I don't know. I've kind of wasted my whole programming session on this unfortunately. should have just done the stupid lein test and moved on with the tutorial. lol. Anyways, thanks for the help folks!

tobias09:10:10

Hi everyone, I have a project that I've been working on for a while and today when I started my repl it randomly gave me an error message that it's never done before. I haven't changed anything in my project. Any idea what could have caused this? https://pastebin.com/9XQ2JV1s I've tried lein clean but that hasn't fixed it. If I start a fresh project then I have no problem with getting the repl running.

jumar09:10:34

@UA9399DFZ it seems like you're loading some image from the Internet in quote_image_maker.core$quotes_from_page invokeStatic core.clj 1354 Perhaps that image is no longer available?

tobias09:10:59

You're right! Thanks, jumar. You saved me from a morning of pulling my hair out 🙂

👍 4
tobias09:10:15

I'd had a local server running yesterday that isn't running today, and it was looking for a file on there.

sb12:10:58

Any idea why didn’t catch the enter key?

sb12:10:41

with other keys, works fine

oddsor12:10:57

I'm not sure what else could be wrong, but at least your if-clause needs to wrap the two bottom lists, right? EDIT: nvm I misread!

sb12:10:35

yes, I would like to catch the ENTER key.. but other keys.. like CTRL, SHIFT.. not works. I don’t know why.

sb12:10:00

I will try out with a pure div, maybe xterm thing..

oddsor12:10:05

could it be that .-key is not a number? according to https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values you'd get "Enter" on an enter-click

sb12:10:33

I check it, thanks!

sb13:10:01

Ok, I ckecked.. that is something “xterm .. problem”.

sb13:10:34

I forget add tabIndex 0, that was the problem

Mattias13:10:48

Hey all, forgive me but... 😅 what is the up to date recommendation for creating REST (micro) services in Clojure? I’ve found answers mostly for several years back and I imagine things move quickly. Looking for something light - web server/URL-to-code mapping but not necessarily much more. New to Clojure, done a lot of Java/Python/etc.

Andrea Imparato13:10:15

I have the same question 😄. I think nowadays for rest api it's liberator vs yada mostly but I'm not an expert as I said 🙂

Mattias13:10:45

Hey, thanks. That’s more information than nothing 😀

frenata13:10:54

https://github.com/razum2um/awesome-clojure#restful-api is probably worth checking out the options there personally I've used (and been happy with) compojure / compojure-api.

Andrea Imparato13:10:47

@U8RCW3A2Y how much did you have to add on your own before having a well structured web api application?

frenata13:10:21

not too much, a jetty server to run the handler

frenata22:10:17

Sorry missed out on this earlier @UCW3QKWKT

frenata22:10:26

from a small prototype I'm working on lately:

(ns monitor.server
  (:require [monitor.handler :refer [handler]]
            [ring.adapter.jetty :refer [run-jetty]])
  (:gen-class))

 (defn -main [& args]
   (let [port (Integer/parseInt "3005")]
     (run-jetty handler {:port port :join? false})))
where handler is a call to compojure.core/defroutes, is a fully functional microservice style server

Mattias16:10:31

Very cool, thank you for elaborating!

Nathaniel Stevens13:10:50

What’s the best way to kick off a bunch of threads and not continue execution until all threads are complete?

markmarkmark13:10:05

You could use a java.util.concurrent.CountDownLatch for that.

markmarkmark13:10:18

as long as you know how many threads you will be starting ahead of time.

orestis13:10:50

Could also kick off a bunch of futures and (mapv deref futures)?

orestis13:10:16

You would also benefit from the built-in ThreadPool (unless you want to have your own).

Nathaniel Stevens13:10:31

These are great suggestions!

Nathaniel Stevens13:10:41

How does mapv defer work?

Nathaniel Stevens13:10:41

I think defer may be cljs only? Is that right?

markmarkmark13:10:00

he typo'd deref

orestis13:10:22

mapv is one way of forcing a lazy seq, but only because I can never remember the differences between doseq, doall etc.

noisesmith17:10:54

if it helps, doseq is like for but for side effects (returns nil, allows multiple forms in its body, is eager), doall takes a lazy-seq and realizes it (but the output is still a lazy-seq and only the top level is forced), dorun forces each element of a lazy seq but it returns nil (thus doesn't use as much heap as doall and is strictly for side effects over a lazy input), run! is like map with two args, but returns nil (strictly for side effects)

Nathaniel Stevens14:10:21

Aha! Beautiful, that’s exactly what I’m looking for. Thanks guys!

Yehonathan Sharvit14:10:58

Is there a way to undefined an existing variable in Clojure?

Eric Scott17:10:19

I'm hoping someone can point me in the right direction here. Code that was working just fine last week has suddenly started complaining about a missing spec.alpha dependency. So I included it in my project( [org.clojure/clojure "1.9.0"][org.clojure/spec.alpha "0.2.176"]). Now when I enter lein run or lein repl I get an error, I think this is the relevant excerpt from the stack trace:

Eric Scott17:10:35

at clojure.lang.Compiler.load(Compiler.java:7379) ... 6 more Caused by: java.lang.IllegalStateException: Attempting to call unbound fn: #'clojure.core/ident? at clojure.lang.Var$Unbound.throwArity(Var.java:43) at clojure.lang.AFn.invoke(AFn.java:32) at clojure.spec.alpha$spec_impl.invokeStatic(alpha.clj:915) I would appreciate any guidance in sorting this out. Thanks.

seancorfield17:10:33

That almost sounds like something is loading an earlier version of Clojure. Are you using Leiningen @eric.d.scott?

seancorfield17:10:02

Could you share your project.clj file? And also check ~/.lein/profiles.clj to see what's in there (if anything).

dpsutton17:10:39

and give the results of *clojure-version* ( i think that's the dynamic var)

Eric Scott17:10:30

Ah! I changed this line in my ~/.lein/profiles.clj :

Eric Scott17:10:32

:dependencies [[org.clojure/clojure "1.9.0"]] ;;"1.8.0"]]

Eric Scott17:10:40

... and it fixed the problem

Eric Scott17:10:19

So do I gather from this that I must always set this to the highest clojure version used by any of my projects?

Eric Scott17:10:16

Is there a risk associated with using older clojure versions from projects started a while back?

hiredman17:10:31

what clojure version do you have specified in your project?

Eric Scott17:10:00

... as generated by the luminus template, I think

hiredman17:10:44

I dunno, I have never really used /.lein/profiles.clj because my use of lein largely predates it, and because of my lack of personal experience with it, all I have is questions like yours to go on, and of course it is always someone asking for help with a problem (people don't ask when things are going well), so given that bias I am of the opinion that anything but an empty /.lein/profiles.clj is asking for trouble

hiredman17:10:25

(and adding to the biased opinion, I have a pretty low opinion of everything people tend to stick in there)

dadair17:10:37

I find it odd to specify a clojure dependency in .lein/profiles. Usually that is best for cross-project user-specific deps/plugins like formatters, linters, scope-capture, etc

dadair17:10:03

And each project would define its own clojure dep in the project.clj/deps.edn/etc

Eric Scott17:10:23

Ah. I started this file months ago following some tutorial (Brave and True maybe?), and haven't thought about it since.

hiredman17:10:04

cross project user specific plugins are a fancy way to make your builds not repeatable

dadair17:10:28

Depends on the profile/scope

dadair17:10:50

I can’t remember a time where cljfmt in a dev profile plugin screwed my build

hiredman17:10:46

shrug the other week there was just a discussion about a popular plugin (ultra maybe?) and how it injects itself as a dependency of the project, which results in the project's generated pom having a dependency on it when you create an artifact

hiredman17:10:17

that kind of thing happens all the time, it is just mostly small enough that it gets ignored

hiredman17:10:29

ultra just happens to have about a billion deps so people notice

dadair17:10:59

imo that’s a red flag on the plugin(s) that forcibly inject themselves as a dep, than a red flag on the utility of a global profiles

noisesmith17:10:18

ultra is weird

dadair17:10:05

But I agree that the more you can put into each project themselves is better, but I personally don’t want to keep a cljfmt plugin up to date for the occasional use across all my projects

andy.fingerhut17:10:11

Certainly commenting most things out of your /.lein/profiles.clj is a reasonable practice if you are having Leiningen dependency headaches, to see if they go away. That an 'lein clean'. I have only lein-ancient and eastwood in my /.lein/profiles.clj most of the time, and unlike ultra I haven't seen them cause any troubles.

noisesmith17:10:34

also under-rated is making up new profile names specifically for a given plugin or dep

noisesmith17:10:46

eg. :perf-profile or :disasemble

andy.fingerhut17:10:57

Regarding spec being a dependency, or a missing one, Clojure 1.8 doesn't implement or include spec unless you use a special back-ported library to add it. Clojure 1.9 depends on spec normally, so if you depend on Clojure 1.9 you should not also need to explicitly add dependences that it brings in for you. If you are doing some special generative testing things you may need additional dependencies, but not just for running Clojure normally.

andy.fingerhut17:10:38

@eric.d.scott ^^^ I meant to ping you on that.

hiredman17:10:33

it isn't that using .lein/profiles.clj is guaranteed to break your builds, it just is a source of potential differences for a dev team that isn't in source control

Eric Scott17:10:34

Ah. Good to know. Thanks.

seancorfield17:10:36

~/.lein/profiles.clj seems to be a big source of beginner problems so it's usually the first thing I'm suspicious about when I see what looks like a dependency problem.

👍 8
andy.fingerhut17:10:21

Regarding your question "Is there a risk associated with using older clojure versions from projects started a while back?" not sure if the following ramblings answer your question -- Across different invocations of the JVM, I think people often have some projects using Clojure version A and others using Clojure version B. The different JVM runs don't affect each other, so whatever versions of Clojure libs they use is independent for each, and the only times they should matter is if those two JVM processes communicate or interact in some way that depends upon those libraries.

andy.fingerhut18:10:05

It is common to have multiple versions of Clojure installed on disk on the same machine at the same time, with no problems for that reason alone.

Eric Scott18:10:43

Yes that makes perfect sense now that I've removed profiles.clj from the mix. Somehow I got the impression from the tutorial I was using that profiles.clj was the norm.

andy.fingerhut18:10:26

With Leiningen, a project-specific project.clj file is the most common way to specify other libs that project uses.

Eric Scott18:10:54

Yes I see that now. Thanks for everyone's help!