Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

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

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:

  1. To avoid executing arbitrary code, data-in reads values from files with clojure.edn/read-string.

  2. 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.

  3. The private function f is called f to match the argument f at the call site in the private function get-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

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:

  1. To avoid executing arbitrary code, data-in reads values from files with clojure.edn/read-string.

  2. 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.

  3. The private function f is called f to match the argument f at the call site in the private function get-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

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:

  1. To avoid executing arbitrary code, data-in reads values from files with clojure.edn/read-string.

  2. 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.

  3. The private function f is called f to match the argument f at the call site in the private function get-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)))
Source Link
ben rudgers
  • 2.7k
  • 13
  • 23

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

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:

  1. To avoid executing arbitrary code, data-in reads values from files with clojure.edn/read-string.

  2. 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.

  3. The private function f is called f to match the argument f at the call site in the private function get-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)))
lang-clj

AltStyle によって変換されたページ (->オリジナル) /