Last active
December 19, 2015 14:19
-
-
Save fbmnds/5968844 to your computer and use it in GitHub Desktop.
Explicite iteration on CLR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require '[clojure.clr.io :as io]) | |
| (def in-file "/path/to/datafile-4gb.tsv") | |
| (def out-file "/path/to/datafile-16gb.tsv") | |
| (defn multiply-string [s n] | |
| (cond | |
| (> n 1) (str s \newline (multiply-string s (dec n))) | |
| (= n 1) (str s \newline) | |
| :else nil)) | |
| (defn multiply-file [in out n] | |
| (with-open [reader (io/text-reader in) ; FB: io/reader | |
| writer (io/text-writer out)] ; FB: io/writer | |
| (loop [line (.ReadLine reader)] ; FB: .readLine | |
| (if (nil? line) | |
| nil | |
| (do | |
| (.Write writer (multiply-string line n)) ; FB: .write | |
| (recur (.ReadLine reader))))))) ; FB: .readLine | |
| #'user/multiply-file | |
| user=> (time (multiply-file in-file out-file 4)) | |
| "Elapsed time: 2638910 msecs" | |
| nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment