Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a file with test data in with Clojure?

Tags:

clojure

I am writing a piece of code that needs to read in a text file that has data. The text file is in the format:

name 1 4
name 2 4 5
name 3 1 9

I am trying to create a vector of a map in the form [:name Sarah :weight 1 cost :4].

When I try reading the file in with the line-seq reader, it reads each line as an item so the partition is not correct. See repl below:

(let [file-text (line-seq (reader "C://Drugs/myproject/src/myproject/data.txt"))
                       new-test-items (vec (map #(apply struct item %) (partition 3 file-text)))]
     (println file-text)   
     (println new-test-items))


(sarah 1 1 jason 4 5 nila 3 2  jonas 5 6 judy 8 15 denny 9 14 lis 2 2  )
[{:name sarah 1 1, :weight jason 4 5, :value nila 3 2 } {:name jonas 5 6, :weight judy 8 15, :value denny 9 14}]

I then tried to just take 1 partition, but still the structure is not right.

=> (let [file-text (line-seq (reader "C://Drugs/myproject/src/myproject/data.txt"))
                                new-test-items (vec (map #(apply struct item %) (partition 1 file-text)))]
              (println file-text)   
              (println new-test-items))
(sarah 1 1 jason 4 5 nila 3 2  jonas 5 6 judy 8 15 denny 9 14 lis 2 2  )
[{:name sarah 1 1, :weight nil, :value nil} {:name jason 4 5, :weight nil, :value nil} {:name nila 3 2 , :weight nil, :value nil} {:name jonas 5 6, :weight nil, :value nil} {:name judy 8 15, :weight nil, :value nil} {:name denny 9 14, :weight nil, :value nil} {:name lis 2 2, :weight nil, :value nil} {:name  , :weight nil, :value nil}]
nil

Next I tried to slurp the file, but that is worse:

=> (let [slurp-input (slurp "C://Drugs/myproject/src/myproject/data.txt")
            part-items (partition 3 slurp-input)
            mapping (vec (map #(apply struct item %) part-items))]
            (println slurp-input)
            (println part-items)
            (println mapping))
sarah 1 1
jason 4 5
nila 3 2 
jonas 5 6
judy 8 15
denny 9 14
lis 2 2

((s a r) (a h  ) (1   1) (

Please help! This seems like such an easy thing to do in Java, but is killing me in Clojure.

like image 324
user1525748 Avatar asked Jul 15 '12 18:07

user1525748


2 Answers

split it into a sequence of lines:

(line-seq (reader "/tmp/data"))

split each of them into a sequence of words

(map #(split % #" ") data)

make a function that takes a vector of one data and turns it into a map with the correct keys

(fn [[name weight cost]] 
   (hash-map :name name 
             :weight (Integer/parseInt weight) 
             :cost (Integer/parseInt cost))) 

then nest them back together

(map (fn [[name weight cost]] 
       (hash-map :name name 
                 :weight (Integer/parseInt weight) 
                 :cost (Integer/parseInt cost))) 
     (map #(split % #" ") (line-seq (reader "/tmp/data"))))

({:weight 1, :name "name", :cost 4} 
 {:weight 2, :name "name", :cost 4} 
 {:weight 3, :name "name", :cost 1})

you can also make this more compact by using zip-map

like image 99
Arthur Ulfeldt Avatar answered Sep 29 '22 05:09

Arthur Ulfeldt


You are trying to do everything in one place without testing intermediate results. Instead Clojure recommends to decompose task into a number of subtasks - this makes code much more flexible and testable. Here's the code for your task (I assume records in file describe people):

(defn read-lines [filename]
  (with-open [rdr (clojure.java.io/reader filename)]
    (doall (line-seq rdr))))

(defn make-person [s]
  (reduce conj (map hash-map [:name :weight :value] (.split s " "))))

(map make-person (read-lines "/path/to/file"))
like image 36
ffriend Avatar answered Sep 29 '22 05:09

ffriend