Skip to content

Instantly share code, notes, and snippets.

@samedhi
Created December 23, 2017 01:28
Show Gist options
  • Save samedhi/cd12a4c6f18be568281334a8e5184174 to your computer and use it in GitHub Desktop.
Save samedhi/cd12a4c6f18be568281334a8e5184174 to your computer and use it in GitHub Desktop.
(ns csv-example
(:require
[clojure.string :as string]))
(def csv
[["name" "age" "occupation" "gender"]
["Peter Pan" 312 "Lost Boy" "male"]
["Wendy Darling" 14 "Older Sister" "female"]
["Captain Hook" 330 "Pirate" "male"]
["Tinker Bell" nil "Fairy" "female"]])
(defn clean-str [s]
(pr-str (string/replace s "\n" "")))
(defn build-csv [xs]
(->> xs
(map (fn [xs] (map #(if (string? %) (clean-str %) %) xs))) ;; quote all strings
(map (partial string/join ",")) ;; seq of seq -> seq of strings
(string/join "\n"))) ;; seq of string -> string
(println (build-csv csv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment