I am only starting off with clojure, and am stuck thinking how to implement a seemingly straightforward functionality.
There is a function generator, which takes (among others) an saver function as an argument. The generator does all sorts of stuff and generates certain data objects regularly, which need to be saved. This saving is supposed to be handled by the saver function, and so the generator calls the saver with the data that needs to be saved, every time data is generated.
Now, one saver function I am to write is one that saves the data to a sqlite db. How should I go about this?
-
One strategy I thought of is to create a connection to the sqlite db in the
saverfunction. Create a new connection every time data is to be saved, save the data (only one row in one table) and close the connection. This seemed to be a bit inefficient. Especially considering the data gets generated every 2-5 secs. -
Another idea is to keep an open connection as a module-level var, which is set to
nilat start. The connection is opened the first time thesaverfunction is called and is reused in the subsequent calls. This seems it would probably be more efficient, but to my knowledge, it would require adefform inside of thesaverfunction. Personally, I don’t enjoy doing that. -
One more (crazy?) thought I had was to use an agent that saves the connection object, initially set to
nil. Thesaverwill be a function thatsends data to the agent. The agent, creates the connection the first time it needs it, and saves in its associated data object. This looks like it might work well, but agents aren’t designed for this, are they?
So, how do you people address problem? Is there any other paradigm suited just for this case? Or should I do one of the above?
PS. I spent a good deal of time writing this as it’s very hard to put my problem in words. I’m not sure if I got it all right. Let me know if something is unclear.
your second solution sounds best. if you don’t want to use a mutable
Var(created viadef) then you could create the connection in a “factory” function as a simple immutable value (so it’s just carried around in the closure):disclaimer: i am no great clojure expert – the above is just how i would do this in pretty much any functional language. so perhaps there is a more idiomatic clojure approach.