Converting Instant Time into Joda Time in Clojure - clojure

I am stuck trying to convert an #inst "2016-08-15T14:00:00.000-00:00" into #object[org.joda.time.DateTime 0x1d4c2099 "2016-08-15T20:47:52.658Z"].
I am trying to do this in order to add (t/minutes duration) to my time/date!
Is there a way to convert the instant into joda time in clojure?

Perhaps (clj-time.coerce/to-date-time my-inst) will do the trick?
the coerce namespace has a bunch of helpers for moving between types.

Related

Why BENCODE has been used for transporting clojure code to nrepl in CIDER?

Why can't we simply convert Clojure code to string and send it over TCP and evaluate on the other side(nrepl)?
For example : This is a hashmap {"foo" "bar", 1 "spam"} whose BENCODE encoding is d3:foo3:bari1e4:spame.
If we convert it to string -> {\"foo\" \"bar\", 1 \"spam\"}
and evaluate on the other side instead of using BENCODE as shown below.
(eval (read-string "{\"foo\" \"bar\", 1 \"spam\"}"))
; ⇒ {"foo" "bar", 1 "spam"}
I am new to the Clojure world. This might be a stupid question but anyway.
nREPL's maintainer here. There are several reasons why nREPL uses bencode by default:
We needed a data format that supports data streaming easily (you won't find many streaming JSON parsers)
We needed a data format that could easily be supported by many clients (support for formats like JSON and EDN is tricky in editors like Emacs and vim). I can tell you CIDER would not have existed if 8 years ago we had to deal with JSON there. :-)
Bencode is so simple that usually you don't even need to rely on a third-party library for it (many clients/servers have their own implementation of the encoding/decoding in less than 100 lines of code) - this means means that clients/servers have one less 3rd party library. Tools like nREPL can't really have runtime deps, as they conflict with the user application deps.
EDN didn't exist back when nREPL was created
Btw, these days nREPL supports EDN and JSON (via the fastlane library) as well, but I think that bencode is still the best transport in most cases.
For people looking for the answer, read the # Motivation section in https://github.com/clojure/tools.nrepl/blob/master/src/main/clojure/clojure/tools/nrepl/bencode.clj
This is very well written.

Get runtime ColdFusion syntax trees?

Is it possible to get access to / modify ColdFusion syntax trees at run time?
I'd wager not, and a 10 minute google search didn't find anything. Fiddling with closures and writing metadata dumps, we can see stringified versions of objects like [runtime expression], for example in the following:
function x(a=b+1) {}
WriteDump(getMetaData(x).parameters[1]["default"]);
Does it allow us to go no deeper than this, or perhaps someone knows how to keep digging and start walking trees?
Default UDF parameter expressions aren't available in function metadata as you've found. Other libraries that have implemented some form of CFML parser are
CFLint (written in Java and using ANTLR)
https://github.com/cflint/CFLint
CFFormat (also uses a binary compiled from Rust)
https://www.forgebox.io/view/commandbox-cfformat
Function LineNums (pure CFML)
https://www.forgebox.io/view/funclinenums
There is also a function callStackGet() docs: https://cfdocs.org/callstackget which might be useful to whatever you are trying to do.
And another CFML parser (written in CFML) here: https://github.com/foundeo/cfmlparser

Using Prismatic Schema and Core.Typed at the same time?

I'm experimenting with these two libraries. I am able to use one or the other on their own, but when I try to use both at the same time (i.e., in the same file) I get an extremely large console error that complains about numerous prismatic schema library functions not being properly annotated. Is there a way to make core.typed ignore the schema stuff?

Implementing Data Frames in OCaml

I have been learning OCaml on my own and I've been really impressed with the language. I wanted to develop a small machine learning library for practice but I've been presented with a problem.
In Python one can use Pandas to load data files then pass it to a library like Scikit-Learn very easily. I would like to emulate the same process in OCaml. However, there doesn't seem to be any data frames library in OCaml. I've checked 'ocaml-csv' but it doesn't really seem to be doing what I want. I also looked into 'Frames' from Haskell but it uses TemplateHaskell but I believe a simpler way to do the same thing should be possible if Pandas can simply load the data file into memory without compile-time metaprogramming.
Does anyone know how data frames are implemented in Pandas or R, a quick search on Google doesn't seem to return useful links.
Is it possible to use a parser generator such as Menhir to parse CSV files? Also, I'm unsure how static typing works with data frames.
Would you have a reference about the format of data frames? It may not be so hard to add to ocaml-csv if CSV is the underlying representation. The better is to open an issue with a request and the needed information.

Writing just a function that saves given objects to sqlite

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 saver function. 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 nil at start. The connection is opened the first time the saver function is called and is reused in the subsequent calls. This seems it would probably be more efficient, but to my knowledge, it would require a def form inside of the saver function. 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. The saver will be a function that sends 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 via def) then you could create the connection in a "factory" function as a simple immutable value (so it's just carried around in the closure):
(defn sqlite-saver-factory [path]
(let [db-connection (open-sqlite-connection path)]
(fn [data]
(save-to-sqlite db-connection data))))
...
(generator (sqlite-saver-factory path) ...)
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.