2
; common/math.clj
(defn nths
"Returns a collection of values for each idx in idxs. Throws an error if any one idx is out of bounds."
[coll idxs]
(map #(nth coll %) idxs))
; nrepl
common.math> (try (/ 1 0)
(catch Exception e (prn "in catch"))
(finally (prn "in finally")))
"in catch"
"in finally"
nil
common.math> (try (nths '(5 6 7 8 9) '(0 5))
(catch Exception e (prn "in catch"))
(finally (prn "in finally")))
"in finally"
IndexOutOfBoundsException clojure.lang.RT.nthFrom (RT.java:784)
common.math> (nths '(5 6 7 8 9) '(0 1 3))
(5 6 8)
common.math> *clojure-version*
{:major 1, :minor 5, :incremental 0, :qualifier "alpha4"}
我无法弄清楚第二个expr有什么问题。我期待它会重新打印:运行单元测试时出现异常未被捕获的异种?
"in catch"
"in finally"
同样的事情:
lein test unittest.common.math
FAIL in (test-nths) (math.clj:87)
expected: (thrown? IndexOutOfBoundsException (nths (quote (5 6 7 8 9)) (quote (0 5))))
actual: nil
应该通过。
有道理。我没有想到repl会试图评估'try'的返回值,也就是来自'(nths'(5 6 7 8 9)'(0 5))''的懒惰列表。 – wleung
我称之为“懒虫”,它以很多方式咬我...... ps:在技术上,REPL只是试图打印这个值,它是懒惰的seq导致它被评估,然后 –