- Application 讀取一般檔案
使用 clojure.java.io/resource ,可以從 classpath 來讀取檔案。檔案放在 /resources 資料夾下即可。 - 「尋找 collections 符合某個條件的 item ,找到後傳回第一個」(find-first)
stackoverflow 列舉了數種寫法
(a) filter/ first -> 最容易理解
(b) some
(c) reduce/ reduced -> 效能最好 - lein repl 啟動時發生 timeout
解決方案: 在 project.clj 裡修改 repl 的 timeout:repl-options { ;; If nREPL takes too long to load it may timeout, ;; increase this to wait longer before timing out. ;; Defaults to 30000 (30 seconds) :timeout 120000 }
- 「讀取 linux 的 /proc 系統檔」
需要使用一些特別的技巧,因為 /proc 下的檔案和一般的檔案不同,要先做一些轉換。
(slurp (java.io.FileReader. "/proc/cpuinfo")) - 將字串轉換成整數值
儘量不要輕易地使用 read-string 這個函數。用比較單純的 (Integer/parseInt "123") - here document
clojure 沒有 here document 的語法。如果要使用的字串裡頭包含了太多需要逸脫 (escape) 的字元,官方建議的作法是把字串放在 /resources 下的檔案裡,用讀檔的方式取得。 - clojure 數值型態 suffix 的意義
(type 1N) => clojure.lang.BigInt
(type 1) => java.lang.Long
(type 1.0) => java.lang.Double
(type 1M) => java.math.BigDecimal
Wednesday, September 13, 2017
clojure 實務技巧
Labels:
clojure