This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-11-07
Channels
- # announcements (37)
- # babashka (28)
- # beginners (104)
- # calva (28)
- # cider (32)
- # clj-kondo (35)
- # cljs-dev (4)
- # cljsrn (3)
- # clojure (35)
- # clojure-conj (4)
- # clojure-dev (57)
- # clojure-europe (4)
- # clojure-france (6)
- # clojure-gamedev (1)
- # clojure-germany (1)
- # clojure-hamburg (2)
- # clojure-italy (7)
- # clojure-nl (4)
- # clojure-spec (9)
- # clojure-uk (11)
- # clojuredesign-podcast (2)
- # clojurescript (36)
- # clojurex (48)
- # core-async (6)
- # cursive (12)
- # data-science (1)
- # datomic (21)
- # defnpodcast (7)
- # duct (1)
- # events (1)
- # fulcro (56)
- # graalvm (30)
- # graphql (5)
- # jobs (1)
- # joker (21)
- # keechma (1)
- # leiningen (4)
- # off-topic (109)
- # parinfer (20)
- # pathom (27)
- # re-frame (4)
- # shadow-cljs (80)
- # spacemacs (18)
- # sql (32)
- # testing (2)
- # tools-deps (32)
- # vim (20)
hi, could you help me please ? I'm a trying to code function that read multiple lines of input (at once) but function only reads the first one
@marcio.akk Share your code and we'll try to help.
(if it's a lot of code, you can put it in a gist on github and just share the link here)
Small code snippets shared here can be surround with triple backticks for formatting
like this
What I'm trying to accomplish: paste a bunch of lines (json) and function reads it. I tried this code, but I seems that it keeps waiting for user input indefinitely
(! 806)-> clj
Clojure 1.10.1
(defn read-json
[]
(println (line-seq (java.io.BufferedReader. *in*))))
#'user/read-json
user=> (read-json)
Hello <-- pressed RETURN here
World <-- pressed RETURN and then control-d here
(Hello World)
nil
user=>
Did you press control-d to signal end of file?
control-d writes ^D in the console
Oh, are you on Windows?
Then it would need to be control-z instead of control-d I think.
PS C:\Users\sean> clj
Clojure 1.10.1
user=> (defn read-json
[]
(println (line-seq (java.io.BufferedReader. *in*))))
#'user/read-json
user=> (read-json)
Hello <-- pressed RETURN here
World <-- pressed RETURN here
^Z <-- pressed control-z and then RETURN here
( Hello World)
nil
user=>
damn 😅
I use pedestal/ mount. When I modified the Clojure part, then everything works just I need to push CTRL+F5 on browser. Now I solved with a watcher script which do that automatically. Is that possible to do more Clojure native way? Browser (one active tab) refresh?
Hi guys, I was wondering how do you guys usually keep your database connection in Clojure?
During my search I stumbled upon this great question: https://stackoverflow.com/questions/39579023/clojure-best-approach-for-singleton-like-data-connection
But it doesn’t address my concern about flexibility of the connection created. I wanted the connection to be global in order to be accessed from anywhere so I was thinking def
or defonce
should do it. But I also want to be able to pass values for creating the connection, I don’t want to rely heavily on configuration reading tools like environ
etc to determine my connection configuration.
This way I heavily depends on env, which is provided by environ
(def conn-options {:jdbc-url (env :database-url)})
(defonce conn (delay (make-datasource conn-options)))
Ideally I want something like
(defn create-connection [& params] (make-datasource params))
But by using it will result in connection being created every time the function is invoked. That’s why I was looking for something like a Singleton.
I realize maybe better approach is possible in Clojure, but being a beginner, this is the only way I can think about.I have a simple example of using next jdbc with integrant here:
. All db calls are handled within one namespace (db.clj)
Adapt as you see fit, i.e., swap out integrant with another solution if suits you better 🙂
I also observed that many people use tools like integrant, mount, component, etc. Why is that?
It's just a simple way of doing IoC in Clojure, where you can inject functions with side-effects into another namespace during "system" initialisation. i.e., setup a jetty webserver and hand that off to other namespaces as required.
which also permits stopping/starting/restarting your "system" within your REPL during development
Don't worry about it for now then. Scratch that itch when you need it - but no need in overly complicating your learning experience with something that you don't need atm.
But maybe that’s the problem, if I don’t have the trouble, I shouldn’t use the tool at all
> Don’t worry about it for now then. Scratch that itch when you need it Well noted, thanks again friend for your kind help
I guess what I was trying to say, is simply keep your database connection in one namespace.
That as well, oh by the way, are you on ClojureVerse as well @U11EL3P9U?
I’ve made a duplicate question there in order to receive more inputs, also to keep me from having to search the question again in the future
Here’s the link if you want to https://clojureverse.org/t/how-to-manage-database-connection-in-clojure/5067
Thank you @U11EL3P9U, you’ve been a very big help!
You're welcome. Bear in mind, that's just one way - i.e., my way 🙂 You may discover a better way, or others may suggest other ways too 🙂
That’s what I’m hoping for. I’ve gotta say, it’s kinda hard looking for beginner problem discussion on Clojure. Whereas in other language you’d find it on top 5-6 results in your search engine.
Oh people here on Slack are 99.9999% friendly, so I hope you'll discover answers to any other issues you may encounter 🙂
Yeah, but searching a problem on Slack isn’t as straightforward as it is on Google, haha
I’ve been on this forum actually, more than a year ago when I started to learn Clojure, but after not getting enough traction in my workplace I didn’t pursue it any longer.
Now, I’m going to give Clojure another chance, who knows, maybe after a year I’ve matured enough and Clojure might seems more familiar this time.
I gotta say, I was a total noob back then when I was starting Clojure, everything seems like some sorts of incantation, lol
which uses the new JUXT Clip library rather than integrant. I've actually moved my stuff over to Clip as it's less intrusive/demanding than Integrant.
(into [] (map :body) (client/get "http://myapi")) returns a channel
That doesn't look right. You need to consume the response in a core.async go
block. There's an example on https://github.com/r0man/cljs-http
I would like to have the response of the http get in a sequence, or in an object, the http response is json.
You might have to deserialize the json from a string, I'm not sure if cljs-http does it automatically.
(go (let [response (<! (http/get ""))]
(do-something-with-your-response response)))
(sequence (take 1) (client/get "http://example.com" {:channel (cljs.core.async/chan 1 (map :body))}))
(go (let [response (<! (http/get "https://api.github.com/users"))] (:body response)))
That is a core.async channel which you shouldn't worry about. You should use the response within the go
block.
Thanks @UCPGSBNQ4
Hi clojurians 😎 I have 2 Java files I want to use in my clojure project, The directory structure is:
myproject/src/java/somelib/Class.java
./somelib/OtherClass.java
I ran lein jar and lein install and it created a jar and a pom file that refers it.
I also added
:java-source-paths ["src/java" "test/java"]
To my project.clj file
But how do I use it from clojure?
Like, how to add a dependency and imports
Thanks!You just use import
or a fully qualified name.
Did you check https://clojure.org/reference/java_interop ?
You can have a look here too: https://github.com/jumarko/clojure-experiments/blob/master/src/clojure_experiments/experiments.clj#L266-L268
Thank you!
The solution was indeed
(import Class)
and
(import someLib.OtherClass)
wasn't sure why i thought it doesn't work
Maybe I needed to also run lein install in the subfolder?
anyways it worked afterwards.
I'm trying to work through some socket examples in the Java Tutorials with Clojure - https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html Is there anything similar to the Java Trails, but with clojure examples that anyone is aware of? My first step is just to get something to work, but then I'd like to understand the "Clojure" way of organizing the code to push the side effects to the edges of the system.
@U3L6TFEJF I really just want to have a way to read a string off the socket's InputStream and pass that to a handler, the hander would return a string that gets written back to the socket's OutputStream. I'm not going to try to implement anything complex.
makes sense. I’m not aware of anything trail-like for this particular thing unfortunately
I guess if I get it working, I can start something on github and get some folks to review it 🙂
@scott.archer anything involving Java Sockets in Clojure is going to result in some gnarly interop. The Socket APIs are some of the oldest in Java and they are written in a very imperative style, so pushing side-effects to the edges with Sockets is going to be hard work.
hi what is a good pattern to test asyncronous code? For example, I have a setup & teardown setup using midje
that create/delete a datomic database for each bundle of facts. However, I am now testing this portion of async code that instantly returns and midje promptly deletes the databse. I would need a way to know when the async processes finished in order to call the delete-database
function. Is there a good pattern to treat it?
I thought about sending a data through the chan
to inform that it was the last one and save this into a table. In the test portion I would be "looking at" the table for the arrival of this information. Does not feel right for me, but I am inexperienced with the async
world
the easiest way to interface core.async code with non-core.async code is using the double bang operations, <!!, >!!, alt!!
it is, in extremely general sense, a good idea for any bit of async code to provide some way to "wait" for it to finish. this is useful for backpressure, for clean shutdowns, and for tests
the most trivial example is something like (async/go ...)
which returns a channel that closes when the block exits
so as long as you provide something like that, in your tests you can use <!!
to order things around async operations
@hiredman it makes total sense.. this is exactly the pattern I have... a go-block processing stuff in a buffered channel
let me ask you, if I call this async function as a provider for a REST API call, I don't desire to make the client to wait for my response. however, I don't know if this could be problematic as many clients start to consume my service.. something like too many threads opened for example. Do you see problems around it?
you won't get too many threads, but you will get an accumulation of go blocks to run in the core.async threadpool, which can cause you to run out of memory given enough of them (it would likely have to be a ton)
I was curious about the backpressure bit of this situation because each call to the API would span a new channel, right? And each channel would manage its own 'queue'
the way to do backpressure is determine some limit on the number of concurrent operations you want to allow, and return an error to clients when that limit is hit, maybe returning a 503 for http
Hi, I don't understand why I can do
(Integer/parseInt "5")
and (map #(Integer/parseInt %) ["5"])
but I can't do (map Integer/parseInt ["5"])