Hacker News new | comments | show | ask | jobs | submit login
Slurping a file in Common Lisp (emmett.ca)
10 points by jdale27 3109 days ago | hide | past | web | 4 comments | favorite



slurp-stream3 looks pretty sloppy...

  (defun slurp-stream3 (stream)
   (with-output-to-string (out)
     (let ((seq (make-array 1024 :element-type 'character
 			   :adjustable t
	 		   :fill-pointer 1024)))
       (loop
        (setf (fill-pointer seq) (read-sequence seq stream))
        (when (zerop (fill-pointer seq))
	  (return))
        (write-sequence seq out)))))
1. The array doesn't need to be adjustable because he's reading no more than 1024 characters at a time.

2. read-sequence and write-sequence ignore the fill-pointer so there's no need for it, much less initialize it to 1024 (also :fill-pointer t would have the same effect of initializing to the length of the vector). He's using the fill-pointer as a variable so he should just use a simple lexical variable.


For a file that big I'd probably do a mmap/MapViewOfFile (depending on how the data were accessed of course). Anyone have a pointer on that for [SB]CL?


The SB-POSIX package has a function that calls out to mmap: (sb-posix:mmap addr length prot flags fd offset)


Enjoyable read. I especially liked the conclusion.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: