5

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
  • 8
    clojure.string is 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. Commented Mar 19, 2015 at 20:28
  • mind to share, why? is this homework? Commented Mar 19, 2015 at 20:54

2 Answers 2

11

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"])
Sign up to request clarification or add additional context in comments.

3 Comments

(re-seq #"[^,]" "abc,d") ;; => ("a" "b" "c" "d") - I think OP wants #"[^,]+"
clojure.data.csv isn't built in. You need to add it as a dependency.
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
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.