Created
December 23, 2017 01:28
-
-
Save samedhi/cd12a4c6f18be568281334a8e5184174 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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