Fork me on GitHub

@oliv assuming you're talking about a Ring application, the recommended solution would be to add middleware that adds your global configuration to the Ring request. That decouples the dependency from the controller and from the service functions you call. It makes the entire call chain both more reusable and easier to test. And it doesn't change the signature of the controller either.


Hi, what's the performance of realizing an element in infinite sequence with many lazy functions stacked upon it. Take generating prime numbers for example, whenever a prime number is found, a remove function is added to the lazy remaining seq, e.g.

remove (fn[x] (= 0 (rem x primeNum))) remaining))
. To my understanding, when an element is realized, lot's of remove functions need to be called, e.g. for 10, remove function for 2, 3, 5 and 7 needed to be called. Do I understand it correctly?


@oliv: another trick you can use is something like the following:


(defn save-explicit [file text]
  (spit file text))

(def save (partial save-explicit “foo.txt”))


I’m, ahem, partial, to using to using partial over anon-fns where the first param stays the same.


What’s the best way to have Clojure web servers communicate with each other?


@pupeno gnatsd has been working well for me, with clj-nats-async


@mccraigmccraig do you host a gnatsd server yourself?


@pupeno yeah, i have a gnatsd cluster deployed alongside the rest of my app components


I’m not sure how well that will work on Heroku.


ah, you might need to find a heroku addon then


Maybe I should use redis pubsub


i've never used it, but i heard redis pubsub was pretty good, probably given the usual redis caveats


@mccraigmccraig what redis caveats? I haven’t use redis yet.


problems are related to HA configurations iirc, so you are fine with a single-sever setup


There’s also PostgreSQL listen/notify


oh, yeah, that would def make sense if you are already using pg


Except that the PG JDBC driver doesn’t seem to support it. sigh


any ferret users around?


@pupeno yes it supports it


niwinz oh, does it?


there are the pgjdbc-ng driver that has better support for it


hi all - quick Q - I am sure there is a more idiomatic approach than (map vector [1 2 3] [2 3 4]) to give [[1 2] [2 3] [3 4]] but I can’t remember it - any ideas?


(conj [] [1 2 3] [4 5 6])?


Honestly that looks pretty idiomatic to me!


I always try to do (zip [1 2 3] [2 3 4]) just to find out that there's no zip in clojure.core 😛


to achieve what (map vector [1 2 3] [2 3 4]) does, that is


but that gives you a map


@pupeno we use redis here, happy with it so far (there are tradeoffs tho compared to the alternatives, ex wrt consistency). We have sort of "remote atoms" connected to redis, so we can "add-watch" etc etc, deref is local/cached, all updates are in the background.


If I'do it now I'd probably choose something more robust tho


@mpenet why watching atoms instead of pubsub?


it's pubsub, it's just has an atom like interface (it's a component you can deref and that has !! fns for all atom equivalent functions)


under the hood it's redis pub/sub for notifications of changes + normal keys for persisting stuff across restarts


I see. Interesting. I just need the pub sub though


if you use carmine for pub/sub on redis be aware it doesnt handle reconnections & failures


you'd have to code this stuff yourself


Oh, good to know.


lettuce (java lib) does


overall it looks like a nice/complete library


@oliv: A very practical reason, rather than theoretical reason, why that code you pasted is bad, is that the save operation occurs when the namespace is read. If you take that code, and try and build an uberjar with it, you’ll see that the save operation actually occurs at compile time. Often these kind of side-effects can actually prevent the uberjar process from finishing too (e.g. if it fires off daeomon threads by maybe starting a web-server). If you want the code to execute when the script is loaded, just make it a -main function or call it from the outside explicitly


also if that happened in a library, users of the library would have no control over when that side effect happened… it would complect that effect, with making the functions available to users.


thanks @miikka and @robert-stuttaford (and yeah, a map is most definitely not what I want here ;-))


hey @seancorfield thx for your answer ( yeah, using ring, your are right ! let’s suppose my service function signup-user

1. verify if email/password are not blank
2. verify if email was already taken by someone else ( check agains database )
3. save the new user ( database )
4. send one message to the queue system ( signup.user ) with the step 3 persisted data
so, if I understood correctly, we are going to create middlewares to assoc with the request map the :database db-connection and the :queue queue-connection my service function signup-user will receive the request and fetch everything from there
(defn signup-user [ { database :database queue :queue } request] 
   ;; code here 
is that correct ? @seancorfield ( and all others who want to join and talk about it )


Something like that, yes. I’d use namespaced keywords, to avoid possible conflicts, and probably add a single key with a configuration map.


In our Ring apps, we have a Component (Stuart Sierra’s library) added to all Ring requests as middleware.


And that Component contains all our DB connection pools, our “environment”, and so on.


that is cool ! and the service functions will receive the component already started and the request


and inside the service function you fetch what you want from the request and pass through the composed functions


In general, we only pass what a function needs but that might be the entire application component or just part of it.


(defn my-controller [{:keys [] :as request] …)


That’s how we prefer to do the destructuring.


much better than pass the request down in the service function


Does anyone know if there's a way to have compojure/httpkit call a specific function in case of a 404? E.g. the route is not defined in defroutes, so call my function instead of responding with a default 404.


nm found it -- (compojure.route/not-found my-404-function) in defroutes


Whats the best way to persist a clojure data structure to disk? Like if I just wish to make a simple log that I keep appending to the end of but I want to have high throughput what should I do?


Do you use something like spit, or do you use FileOutputStreams, or FileChannels? And what is the recommended way of serializing the data so it can be written.

Alex Miller (Clojure team)20:11:39

streams are byte-oriented, so generally you want to use something like a Java reader (character-oriented)

Alex Miller (Clojure team)20:11:54

and you can use pr-str to get a data structure as a string


Ok, and what about reading from a file


is there a way to read and write simultaniously?


ideally I could have something which only has to write, but always writes to the end of the log


then separately I wan’t to read from that same log, but I was hoping to have many readers and then can keep track of their own place in the log


maybe after each read you return the offset in the file that you are on so you can pick back up where you left off