Skip to content

Instantly share code, notes, and snippets.

View angerman's full-sized avatar
🇸🇬

angerman

🇸🇬
  • Singapore, Singapore
View GitHub Profile
(in-ns 'clojure.contrib.duck-streams)
(import '(java.io File RandomAccessFile))
;; copied and adapted from c.c.ds
(defmulti #^{:arglist '([x])} random-access-reader class)
(defmethod random-access-reader File [#^File x]
(RandomAccessFile. x "r"))
(defmethod random-access-reader String [#^String x]
(random-access-reader (file-str x)))
"upps.data.model")) line (.readLine raf)] nil)
; Evaluation aborted.
user> (let [raf (random-access-reader (model-file "upps.data.model"))] raf)
#<RandomAccessFile java.io.RandomAccessFile@415b15ab>
user> (let [raf (random-access-reader (model-file "upps.data.model")) line (.readLine raf)] nil)
; Evaluation aborted.
user>
user> (compute-metrics [{} *body*])
"Elapsed time: 0.13 msecs"
"Elapsed time: 0.093 msecs"
user> (set! *warn-on-reflection* true)
true
user> (def *body* (take 10 (drop-header (model-file "upps.data.model"))))
#'user/*body*
(use 'clojure.contrib.duck-streams)
(in-ns 'clojure.contrib.duck-streams)
(import '(java.io File RandomAccessFile))
(import 'BufferedRandomAccessFile)
;; copied and adapted from c.c.ds
(defmulti #^{:arglist '([x])} random-access-reader class)
(defmethod random-access-reader File [#^File x]
(BufferedRandomAccessFile. x "r"))
/** A subclass of RandomAccessFile to enable basic buffering to a byte array
* Copyright (C) 2009 minddumped.blogspot.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
(with-random-access-reader "/Users/angerman/upps.data.model"
(println (next-line)) ;; first line
(println (next-line)) ;; second line
(save-excursion
(println (subs (last (lazy-rest)) 0 20))) ;; last line's first 20 char
(println (next-line)) ;; third line
(println (subs (last (lazy-rest)) 0 20))) ;; last lines first 20 char
(defmacro timer
[expr]
`(let [start# (. System (nanoTime))
ret# ~expr]
(/ (double (- (. System (nanoTime)) start#)) 1000000.0)))
(defmacro statistics [n expr]
`(let [timings# (take ~n (repeatedly (fn [] (timer ~expr))))
min# (apply min timings#)
max# (apply max timings#)
(use 'clojure.contrib.duck-streams)
(in-ns 'clojure.contrib.duck-streams)
(import '(java.io File RandomAccessFile))
(import 'BufferedRandomAccessFile)
;; copied and adapted from c.c.ds
(defmulti #^{:arglist '([x])} random-access-reader class)
(defmethod random-access-reader File [#^File x]
(BufferedRandomAccessFile. x "r"))
/** A subclass of RandomAccessFile to enable basic buffering to a byte array
* Copyright (C) 2009 minddumped.blogspot.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
user> (statistics 25 (with-open [rar (random-access-reader (model-file "upps.data.model"))] (doseq [line (lazy-rest rar)] nil)))
Runs: 25
Min: 1740.527
Max: 2911.521
Mean: 2516.340
Median: 2326.024
sd: 424.961