taruss
4/14/2015 11:02:00 PM
On Monday, April 13, 2015 at 4:39:53 PM UTC-7, WJ wrote:
> JShrager wrote:
>
> > (defun nfloaddirdata (file o dates?)
> > (with-open-file
> > (i file)
> > (let ((headline (read-line i)))
> > (format o "(~a~%" (subseq headline 0 (1- (length headline)))))
> > (loop as line = (read-line i nil nil)
> > until (null line)
> > as j from 1 by 1
> > do (when (zerop (mod j 5)) (format o "~%"))
> > ;; Since everything's in the SAME FORMAT I can use very specific
> > ;; parsing instead of having to split the line. This could be made even
> > ;; more efficient by re-using string arrays, and prob. other techniques.
One such technique would be to avoid creating new strings below by using SUBSEQ and format. Instead one could use WRITE-STRING with optional arguments, although it would be more verbose.
> > (let* ((comma-pos-1 (position #\, line))
> > (comma-pos-2 (position #\, line :start (1+
> > comma-pos-1))))
> > (if dates?
> > (format o " (~a ~a ~s) "
> > (subseq line 0 comma-pos-1)
> > (subseq line (1+ comma-pos-1) comma-pos-2)
> > (subseq line (1+ comma-pos-2)))
For example:
(write-string o " (")
(write-string line 0 :end comma-pos-1)
(write-string o " ")
(write-string line 0 :start (1+ comma-pos-1) :end comma-pos-2)
(write-string o " ")
(write-string line 0 :start (1+ comma-pos-2))
(write-string o ") ")
> > ;; Use dots to save cons cells if we don't need the date.
> > (format o " (~a . ~a) "
> > (subseq line 0 comma-pos-1)
> > (subseq line (1+ comma-pos-1) comma-pos-2)
> > )
> > )))
> > (format o "~% )~%")))
>
> Gauche Scheme:
>
> (use srfi-13) ; string-drop-right
> (use srfi-42) ; do-ec
>
> (define (load-data file oport dates?)
> (call-with-input-file file
> (lambda (iport)
> (format oport "(~a~%" (string-drop-right (read-line iport) 1))
> (do-ec (:parallel (:port line iport read-line)
> (:list j (lrange 1)))
> (begin
> (when (zero? (mod j 5)) (newline oport))
> (let1 fields (string-split line #\,)
^^^^^^^^^^^^^^^^^^^^^^
Fails the requirement to not split the line, presumably out of a desire
for a more efficient parsing usage.
> (if dates?
> (apply format oport " (~a ~a ~s) " fields)
> (apply format oport " (~a . ~a) " (take fields 2))))))))
> (format oport "~% )~%"))