【Clojure 基本知识】小技巧s
;;模拟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 ) )