【问题标题】:Idiomatic Clojure function alias惯用的 Clojure 函数别名
【发布时间】:2016-03-17 15:56:21
【问题描述】:

在 Clojure 中为函数起别名的最惯用方式是什么? 这两种方法在执行方面有什么区别吗?

示例取自Om(Clojurescript,但 Clojure 中的语法相同):

(defn query->ast
  "Given a query expression convert it into an AST."
  [query-expr]
  (parser/query->ast query-expr))

(def query->ast
  "Given a query expression convert it into an AST."
  parser/query->ast)

【问题讨论】:

  • 第二种方法是惯用的答案。

标签: clojure clojurescript idioms


【解决方案1】:

比起defn,我更喜欢def

defn 版本

  • 有一个插入的函数调用,可以省略也可以不省略;
  • 仅限于 arity 1,而 def 版本具有 原件。

【讨论】:

  • 你可以在新的defn 中构建多参数,但这不值得麻烦;你的第一个理由就足够了。
猜你喜欢
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 2012-02-24
相关资源
最近更新 更多