From the README.md
Introduction:
The purpose of data-in
is to read all the lines from a text file and return the file's contents as a Clojure value in a standardized manner.
Specification:
###Takes:
Takes:
Filename
###Assumes:
Assumes:
- a space delimited text file
###Returns:
Returns:
- Vector [L1,L2,...Ln] where Li:
- represents a line in
Filename
- is a vector
- contains EDN values
- represents a line in
Example:
If my-file.txt
contains:
3 9
5 4
6 5
3 2
then (data-in "./my-file.txt")
returns:
[[3 9] [5 4] [6 5] [3 2]]
Comments:
To avoid executing arbitrary code,
data-in
reads values from files withclojure.edn/read-string
.The input is not read lazily. The assumption being that sorting or an equivalent operation will be performed. The basis for this assumption is the proximate use case: reading data files for Discreet Optimization @ Coursera.
The private function
f
is calledf
to match the argumentf
at the call site in the private functionget-data
. This reflects a personal preference for mathematical notation when invoking mathematical abstractions.
#Code:
Code:
(ns data-in.core
(:require [clojure.java.io :as io]
[clojure.edn :as edn]))
(defn- get-data
[data-file f]
(with-open [rdr (clojure.java.io/reader data-file)]
(doall (map f (line-seq rdr)))))
(defn- f
[line]
(vec (map (fn [x] (edn/read-string x))
(clojure.string/split line #" "))))
(defn data-in
[filename]
(vec (get-data filename f)))
From the README.md
Introduction:
The purpose of data-in
is to read all the lines from a text file and return the file's contents as a Clojure value in a standardized manner.
Specification:
###Takes:
Filename
###Assumes:
- a space delimited text file
###Returns:
- Vector [L1,L2,...Ln] where Li:
- represents a line in
Filename
- is a vector
- contains EDN values
- represents a line in
Example:
If my-file.txt
contains:
3 9
5 4
6 5
3 2
then (data-in "./my-file.txt")
returns:
[[3 9] [5 4] [6 5] [3 2]]
Comments:
To avoid executing arbitrary code,
data-in
reads values from files withclojure.edn/read-string
.The input is not read lazily. The assumption being that sorting or an equivalent operation will be performed. The basis for this assumption is the proximate use case: reading data files for Discreet Optimization @ Coursera.
The private function
f
is calledf
to match the argumentf
at the call site in the private functionget-data
. This reflects a personal preference for mathematical notation when invoking mathematical abstractions.
#Code:
(ns data-in.core
(:require [clojure.java.io :as io]
[clojure.edn :as edn]))
(defn- get-data
[data-file f]
(with-open [rdr (clojure.java.io/reader data-file)]
(doall (map f (line-seq rdr)))))
(defn- f
[line]
(vec (map (fn [x] (edn/read-string x))
(clojure.string/split line #" "))))
(defn data-in
[filename]
(vec (get-data filename f)))
From the README.md
Introduction:
The purpose of data-in
is to read all the lines from a text file and return the file's contents as a Clojure value in a standardized manner.
Specification:
Takes:
Filename
Assumes:
- a space delimited text file
Returns:
- Vector [L1,L2,...Ln] where Li:
- represents a line in
Filename
- is a vector
- contains EDN values
- represents a line in
Example:
If my-file.txt
contains:
3 9
5 4
6 5
3 2
then (data-in "./my-file.txt")
returns:
[[3 9] [5 4] [6 5] [3 2]]
Comments:
To avoid executing arbitrary code,
data-in
reads values from files withclojure.edn/read-string
.The input is not read lazily. The assumption being that sorting or an equivalent operation will be performed. The basis for this assumption is the proximate use case: reading data files for Discreet Optimization @ Coursera.
The private function
f
is calledf
to match the argumentf
at the call site in the private functionget-data
. This reflects a personal preference for mathematical notation when invoking mathematical abstractions.
Code:
(ns data-in.core
(:require [clojure.java.io :as io]
[clojure.edn :as edn]))
(defn- get-data
[data-file f]
(with-open [rdr (clojure.java.io/reader data-file)]
(doall (map f (line-seq rdr)))))
(defn- f
[line]
(vec (map (fn [x] (edn/read-string x))
(clojure.string/split line #" "))))
(defn data-in
[filename]
(vec (get-data filename f)))
Read Space Delimited Text File to Standardized Data Type
From the README.md
Introduction:
The purpose of data-in
is to read all the lines from a text file and return the file's contents as a Clojure value in a standardized manner.
Specification:
###Takes:
Filename
###Assumes:
- a space delimited text file
###Returns:
- Vector [L1,L2,...Ln] where Li:
- represents a line in
Filename
- is a vector
- contains EDN values
- represents a line in
Example:
If my-file.txt
contains:
3 9
5 4
6 5
3 2
then (data-in "./my-file.txt")
returns:
[[3 9] [5 4] [6 5] [3 2]]
Comments:
To avoid executing arbitrary code,
data-in
reads values from files withclojure.edn/read-string
.The input is not read lazily. The assumption being that sorting or an equivalent operation will be performed. The basis for this assumption is the proximate use case: reading data files for Discreet Optimization @ Coursera.
The private function
f
is calledf
to match the argumentf
at the call site in the private functionget-data
. This reflects a personal preference for mathematical notation when invoking mathematical abstractions.
#Code:
(ns data-in.core
(:require [clojure.java.io :as io]
[clojure.edn :as edn]))
(defn- get-data
[data-file f]
(with-open [rdr (clojure.java.io/reader data-file)]
(doall (map f (line-seq rdr)))))
(defn- f
[line]
(vec (map (fn [x] (edn/read-string x))
(clojure.string/split line #" "))))
(defn data-in
[filename]
(vec (get-data filename f)))