xia*_*owl 3 functional-programming clojure
我写了一段代码来计算一行的主要哈希(#)字符,这很像一个标题行 Markdown
### Line one -> return 3 ######## Line two -> return 6 (Only care about the first 6 characters.
版本1
(defn
count-leading-hash
[line]
(let [cnt (count (take-while #(= % \#) line))]
(if (> cnt 6) 6 cnt)))
Run Code Online (Sandbox Code Playgroud)
版本2
(defn
count-leading-hash
[line]
(loop [cnt 0]
(if (and (= (.charAt line cnt) \#) (< cnt 6))
(recur (inc cnt))
cnt)))
Run Code Online (Sandbox Code Playgroud)
我曾经time测量过这两种实现方式,发现基于的第一个版本take-while比版本2快2倍."###### Line one"作为输入,版本1需要0.09毫秒,版本2需要大约0.19毫秒.
问题1.是否会recur减慢第二次实施的速度?
问题2.版本1更接近函数式编程范式,是吗?
问题3.您更喜欢哪一个?为什么?(欢迎您编写自己的实现.)
在阅读了cloujure的文档之后,我想出了这个函数的新版本,我认为它很清楚.
(defn
count-leading-hash
[line]
(->> line (take 6) (take-while #(= \# %)) count))
Run Code Online (Sandbox Code Playgroud)
小智 6
我会写这样的函数:
(defn count-leading-hash [line]
(count (take-while #{\#} (take 6 line))))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
139 次 |
| 最近记录: |