Idiomatic way to write Clojure code for repeatedly reading lines from the console?

后端 未结 3 1687
余生分开走
余生分开走 2021-02-06 00:06

Recently I was writing a little CLI script which needed to repeatedly read dates from the console (the number of dates to read was calculated and could be different each time).

3条回答
  •  有刺的猬
    2021-02-06 00:39

    read-line returns nil when the end of file is reached. On the console that is when you press CTRL-d (CTRL-z on Windows).

    You could use this code to take advantage of this:

    (doseq [line (repeatedly read-line) :while line]
        (do-something-with line))
    

    If you must read a fixed number of lines you can use:

    (repeatedly n read-line)
    

提交回复
热议问题