I'm still new in Clojure; I'm trying to split the values parsed from CSV file but without using clojure.string/split lib or any other lib just clojure.core, please some help, thanks in advance.
2 Answers
you can accomplish the same result with re-seq and string/split
user> (clojure.string/split "a,b,c,d,e" #",")
["a" "b" "c" "d" "e"]
user> (re-seq #"[^,]+" "a,b,c,d,e")
("a" "b" "c" "d" "e")
Both of these are available with no dependencies so there is little reason not to use string/split in many cases.
parsing CSV is also a good choice if you are willing to add a dependency:
user> (require '[clojure.data.csv :as csv])
nil
user> (csv/read-csv "A,B,C\n1,2,3\n4,5,5")
(["A" "B" "C"] ["1" "2" "3"] ["4" "5" "5"])
3 Comments
Kyle
(re-seq #"[^,]" "abc,d") ;; => ("a" "b" "c" "d") - I think OP wants #"[^,]+"amalloy
clojure.data.csv isn't built in. You need to add it as a dependency.
Arthur Ulfeldt
oops, thats clojure.data.json/read-str not csv thats in by default. I'll take that last bit out of the answer. thanks for checking up
If your hands were tied and couldn't use clojure.string or clojure.data.csv or re-seq or interop:
(defn comma-separate [s]
(->> s
(partition-by #{\,})
(take-nth 2)
(map #(apply str %))))
(comma-separate "foo,bar") ;; ("foo" "bar")
I agree that you should use clojure.string or clojure.data.csv
clojure.stringis part of the "core" library - there are no external dependencies needed to use it. Why not use it? If this is an exercise, please show us what you have tried.