我有两个版本的函数来计算前导哈希(#)字符,哪个更好?

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.您更喜欢哪一个?为什么?(欢迎您编写自己的实现.)

--Update--

在阅读了cloujure文档之后,我想出了这个函数的新版本,我认为它很清楚.

(defn
  count-leading-hash
  [line]
  (->> line (take 6) (take-while #(= \# %)) count))
Run Code Online (Sandbox Code Playgroud)

小智 6

  1. IMO对小块代码进行时间测量是没有用的
  2. 是的,版本1更具功能性
  3. 我更喜欢版本1,因为它更容易发现错误
  4. 我更喜欢版本1,因为代码更少,因此维护成本更低.

我会写这样的函数:

(defn count-leading-hash [line]
  (count (take-while #{\#} (take 6 line))))
Run Code Online (Sandbox Code Playgroud)