Fork me on GitHub
Grigory Shepelev05:12:54

It might be a stupid question. And it might me formulated incorrectly. But how do I run a channel in endless loop in my main function? I can do that in repl with (async/go-loop ...) but not with lein run

Grigory Shepelev06:12:09

Ok. I just figured out. It was not a problem of my channels misunderstaning, but wrong config.

Grigory Shepelev06:12:37

Now another question. A file being read in my code. How do I say it not to look for the same file to read after compilation, but rather have it "remembered"?

Timur Latypoff08:12:04

I think you could read the file inside a macro, and substitute the reading with actual values you read at compile time. Another approach would be to put the file you’re reading as a resource which is compiles as a part of the final JAR — so it’s always with you the way you compiled it.

Grigory Shepelev08:12:34

If I can somwhow pack it inside of jar?

Timur Latypoff08:12:13

Yes. With leiningen you do it by putting in resource/ folder in your project, and open the file with this function

Grigory Shepelev08:12:15

yep. The provlem is I am doing exactly same thing by now with

(defmacro compile-time-read-file
  (slurp (io/resource resource-path)))

Grigory Shepelev08:12:52

ok. I got it.

👍 1
Grigory Shepelev07:12:25

Ok. I just get this. The problem is still with the first question.


I'm using next.jdbc to connect to postgres which is running as a docker container. I am able to connect to it using intellij and psql, but not via the repl. I have the following in my project.clj

[org.postgresql/postgresql "42.2.5"]
[seancorfield/next.jdbc "1.1.613"]
This is the code I executed to test
  (require '[next.jdbc :as jdbc])
  (def db {:dbtype    "postgresql"
           :classname "org.postgresql.Driver"
           :dbname    "foo-dev"
           :username  "foo"
           :password  "foo"
           :host      "localhost"
           :port      7432})
  (def ds (jdbc/get-datasource db))
  (jdbc/execute! ds ["select * from some-table"]))
I am able to connect to it via psql (entering the password on prompt)
psql -h localhost -p 7432 --username foo -d foo-dev
The error I get when I run (jdbc/execute! ds ["select * from some_table"]) is this
Execution error (ConnectException) at (
Connection refused (Connection refused)


This is the docker-compose.yml part for the postgres container

    image: postgis/postgis:13-master
      - db_data:/var/lib/postgresql/data
      - 7432:5432
      POSTGRES_USER: foo
      POSTGRES_DB: foo-dev


Change username to user


user=> (def db {:dbtype "postgres" :classname "org.postgresql.Driver" :dbname "foo-dev" :user "foo" :password "foo" :host "localhost" :port 7432})
user=> (jdbc/execute! (jdbc/get-datasource db) ["select now()"])
[{:now #inst "2020-12-26T11:25:58"}]


still the same error 😕


Interesting. Works for me, and I basically copy and pasted your code to my terminal


Tried it in the older clojure.jdbc and it works, guessing it's a problem with next.jdbc and my system

(def idb {:classname                                  "org.postgresql.Driver"
          :subprotocol                         "postgresql"
          :subname                             "//localhost:7432/foo-dev"
          :user                                "foo"
          :password                            "foo"})


Missed something important, had to use host.docker.internal instead of localhost since I was testing the port forwarded db connection. Works now, sorry my bad


Glad you got it sorted! 🙂


❯ docker ps                CONTAINER ID   IMAGE                       COMMAND                  CREATED         STATUS         PORTS                    NAMES
04d4199d1cd8   postgis/postgis:13-master   "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes>5432/tcp   david_db_1


Is clojure.lang.PersistentQueue still the appropriate data structure to use if you want a FIFO queue? i.e. conj to the back, pop off the front?


AFAIK that is its primary purpose

👍 1

NB - don't ever use first / rest / etc. seq operations on a queue, it can quietly turn your fifo into a lifo


only use into, peek, conj, and pop


(and other tools built on those)

Andrew Byala16:12:32

Hey, everyone. I'm coming in from the Java/Kotlin world, and I'd like to try building a REST API using Clojure. In Java, you download Spring Boot and get to work, but I'm reading that in the Clojure world you tend to put together the pieces that you want, with everyone using Ring. Is there a de facto standard for building APIs nowadays? I I've seen samples with Reitit and Compojure; where is the industry heading?


compojure and reitit are routers, ring provides an http server (which can be combined with the router of your choice). none of these are REST specific


@abyala for "batteries included" opinionated setups, the biggest player is luminus, they have a project template generator which accepts args reflecting the setup you are looking for (eg. cljs webapp support, database backend...) - the excellent book "web development with clojure" uses a luminus template, walks through building a full app, and is worth the price


but depending on what kind of REST API you are trying to make, there are tools that simplify that as well (and those can be combined with the big pile of code that luminus hands you)

Andrew Byala17:12:20

Awesome, I'll check that out. Thanks!


@popeyepwr for many functions that take variable argument counts, adding an arg to the function is equivalent to another call - in that case apply and reduce do the same thing


so "can" - for many vararg functions that treat their arguments homogenously, you can use either


in practice reduce has some optimizations that the function might not, so it's often preferable


concretely: (+ 1 2 3) will always be the same as (+ (+ 1 2) 3), so apply and reduce will do the same thing with + and [1 2 3]


also conj

(ins)user=> (apply conj [] [1 2 3])
[1 2 3]
(ins)user=> (reduce conj [] [1 2 3])
[1 2 3]


but with apply, it can be smarter about building the result, with reduce it needs to do more allocations (it builds a bunch of immutable vectors it only needs once)


for correctness they both work, it is case by case which is more efficient

👍 1