Loading a CSV into clojurescript with macros?

Hey folks, I’m trying to read and process a CSV in a macro so that I just get edn loaded into my clojurescript, but I keep getting arity <number of lines of csv + 6> errors. Anybody know what’s going on?

In cljs:

(def csv-data
  (dm/read-csv-file "data/awesome.csv")))

In macros.clj:

(ns demo.macros
  (:require [clojure-csv.core :as csv]))

(defmacro read-csv-file [file]
  (doall (csv/parse-csv (slurp file))))

I see the file parsed in the compiled js, but I don’t know how I’m reading it wrong. (edited)

The generated Javascript has this form:

devcards.core_card.csv_data = cljs.core.first((function (){
   var G__31138 = cljs.core.PersistentVector.fromArray(["things","in","the","csv"], true);
var fexpr__31137 = cljs.core.PersistentVector.fromArray(["other","things","in","csv"], true);
return (fexpr__31137.cljs$core$IFn$_invoke$arity$1090 ? fexpr__31137.cljs$core$IFn$_invoke$arity$1090(G__31138,......,G__32227));

I have the feeling I’m not getting the data type I expect to be getting from this, but I’m not exactly sure how to find out and what do do about it.

What is the best way to slurp in a file, process it, and just load it into your clojurescript without having it be re-processed each time?

I think your macro emits a list (as if written in parens) which gets evaluated as a function call with a ton of arguments. If that’s the case, I recommend replacing doall with vec:

(defmacro read-csv-file [file]
  (vec (csv/parse-csv (slurp file))))

… or quoting:

(defmacro read-csv-file [file]
  `(quote ~(doall (csv/parse-csv (slurp file)))))

That’s exactly it! How did you know it was the problem?

The arity <number of lines of csv + 6> error you described was the clue - arity-error => function call, whereas your macro should not emit any function calls.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.