Fork me on GitHub
Marcio Arakaki00:11:07

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

Marcio Arakaki01:11:49

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 ( *in*))))
user=> (read-json)
Hello <-- pressed RETURN here
World <-- pressed RETURN and then control-d here
(Hello World)


Did you press control-d to signal end of file?

Marcio Arakaki01:11:15

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 ( *in*))))
user=> (read-json)
Hello <-- pressed RETURN here
World <-- pressed RETURN here
^Z <-- pressed control-z and then RETURN here
( Hello World)

😅 4
👍 4

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: 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 🙂


Thanks! It’s exactly what I was looking for


You're welcome.


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.


A way of describing your "system" through data


which also permits stopping/starting/restarting your "system" within your REPL during development


it's just a tool - you don't need integrant et al, if you don't need it.


I still don’t quite grasp how it works and in what situation would I want to use that


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


There isn't anything special in my simple example that needs integrant


> 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


If you don’t mind, would you post your answer to the post as well?


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.


welcome back!


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


btw, I've just pushed up a new repo


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.


super simples


You may want to consider that library if you're investigating IoC stuff.


Thanks for letting me know, I’ll definitely check it out


A beginner question, I'm using cljs for first time. I used in the past clj-http


in cljs, cljs-http return a chanel when doing an http get


so I would like to have the body of the response in a sequence (is a json)


(into [] (map :body) (client/get "http://myapi")) returns a channel


I'm so confused


Is that right?

Jan K13:11:20

That doesn't look right. You need to consume the response in a core.async go block. There's an example on


I ll been followin the example, how could I get a sequence out of that go block

Jan K13:11:28

You're making only one request. What kind of sequence do you want?


I would like to have the response of the http get in a sequence, or in an object, the http response is json.


does it makes sense?

Jan K14:11:41

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)))


What is that go block returing?


(sequence (take 1) (client/get "" {:channel (cljs.core.async/chan 1 (map :body))}))


This does not seems correct, I dont understand why


(go (let [response (<! (http/get ""))] (:body response)))


return this #object[cljs.core.async.impl.channels.ManyToManyChannel]


what is that?

Jan K16:11:33

That is a core.async channel which you shouldn't worry about. You should use the response within the go block.


Im making one request and thats it


I tired this but still return a chanel


Hi clojurians 😎 I have 2 Java files I want to use in my clojure project, The directory structure is:

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 ?


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 - 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 🙂


that probably the best way :thumbsup:


@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 channels don't matter


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"])


because (Integer/parseInt X) is java interop. Integer/parseInt is not a clojure function


Java static methods cannot automatically be turned into Clojure functions


Thanks 😄

💯 4

#(Integer/parseInt %) <-- a Clojure function that calls the static method inside