Search code examples
pythonlispcommon-lispclisp

Need help getting CLisp to read standard input into a list


I'm working on converting some existing Python code to CLisp just as an exercise ...

The program reads a list of numbers and creates mean, min, max and standard deviation from the list. I have the file-based function working:

(defun get-file (filename)
   (with-open-file (stream filename)
     (loop for line = (read-line stream nil)
      while line
      collect (parse-float line))))

This works when I call it as

(get-file "/tmp/my.filename")

... but I want the program to read standard input and I've tried various things with no luck.

Any advice?


Solution

  • Just separate concerns:

    (defun get-stream (stream)
      (loop for line = (read-line stream nil)
            while line
            collect (parse-float line)))
    
    (defun get-file (filename)
      (with-open-file (stream filename)
        (get-stream stream)))
    

    Then you can use get-file like you already do, and (get-stream *standard-input*).