【Clojure 基本知識】小技巧s

weixin_34198881發表於2018-10-09

;;模擬console原位更新輸出

;;空格擦除法,輸出空格,是為了擦除短字串尾部沒有佔用的位置,因為退格只是回退,並不刪除
(dotimes [_ 10](let [n (rand) sn (.substring (str n ) 2) len (count sn)](print sn)(flush)(Thread/sleep 500) (dotimes[_ len](print "\b"))(flush)))

;;回退鍵配合刪除字元,由於刪除後,游標又前進一位,所以需要再次回退
(dotimes [_ 10](let [n (rand) sn (.substring (str n ) 2) len (count sn)](print sn)(flush)(Thread/sleep 500) (dotimes[t len](print "\b\u007f\b"))(flush)))

 ;;case巨集,多個匹配,同一返回值:使用list

(let [n 12,unit "hour"]
  (case unit
      ("h" "hour" "hours") (* n 60 60 1000)
      ("m" "min" "minute" "minutes") (* n 60 1000)
      ("s" "sec" "second" "seconds") (* n 1000)
      ("ms" "milisecond" "miliseconds") n
      n
    )
)