Clojure - Side Effects Happening Out Of Order

后端 未结 2 801
粉色の甜心
粉色の甜心 2020-12-06 10:37

While dabbling in Clojure I\'ve written a very basic program to echo whatever the user types into it. However, it doesn\'t run in a way that I\'m perceiving to be natural. H

相关标签:
2条回答
  • 2020-12-06 11:12

    I know nothing of clojure but this sounds like a case of buffers not getting flushed. Figure out how to flush standard out after the print. The println function probably flushes at the end of each line. Try:

    (defn goo []
      (print "echo> ")
      (flush )
      (def resp (read-line))
      (print resp)
    )
    
    0 讨论(0)
  • 2020-12-06 11:23

    Also, please don't use "def" unless you really, really want to define a global variable. Use "let" instead:

    (defn goo []
      (print "echo> ")
      (flush)
      (let [resp (read-line)]
        (print resp)
        (flush)))
    

    or, shorter

    (defn goo []
      (print "echo> ")
      (flush)
      (print (read-line))
      (flush))
    
    0 讨论(0)
提交回复
热议问题