Clojure doesn't provide direct support for named arguments, which are supported under some popular dynamic languages like Ruby, Python. The following Python code was exerpted from `The Joy of Clojure':
def slope(p1=(0,0), p2=(1,1)):
return (float(p2[1] - p1[1])) / (p2[0] - p1[0])
slope((4,15), (3,21))
#=> -6.0
slope(p2=(2,1))
#=> 0.5
slope()
#=> 1.0
It calculates the slope of a line, which can be determined by two given points. The following Clojure code mimics the named arguments:
(defn slope
([] (slope [0 0] [1 1]))
([{:keys [p1 p2] :or {p1 [0 0] p2 [1 1]}}] (slope p1 p2))
([p1 p2]
(/ (- (float (p2 1)) (p1 1)) (- (p2 0) (p1 0)))))
(slope [4 15] [3 21])
;=> -6.0
(slope {:p2 [2 1]})
;=> 0.5
(slope)
;=> 1.0
I think it's sufficient for most of the cases. However, if you want to go deeper, let's use the defnk from clojure.contrib.def.
-------------------------
clojure.contrib.def/defnk
([fn-name & fn-tail])
Macro
Define a function accepting keyword arguments. Symbols up to the first
keyword in the parameter list are taken as positional arguments. Then
an alternating sequence of keywords and defaults values is expected. The
values of the keyword arguments are available in the function body by
virtue of the symbol corresponding to the keyword (cf. :keys destructuring).
defnk accepts an optional docstring as well as an optional metadata map.
Unfornately, no examples were given in the doc string. Here's a simple example:
(require '[clojure.contrib.def :as def])
(def/defnk t [a :b 1] [a b])
(t 3)
;=> [3 1]
(t 3 :b 4)
;=> [3 4]
It should be noted that, the optional/named arguments must be put at the tail of the argument list. i.e. You can't write this: (def/defnk t [a :b 1 c] [a b c]) If you do, weird thing would happen. Or, your function may not even compile.
(def/defnk t [a :b 1 c] [a b c])
(t 1 2 3)
;=> [1 1 nil]
(def/defnk t [a :b 1 c d] [a b c d])
;=> java.lang.Exception: Unable to resolve symbol: d in this context
分享到:
相关推荐
关于网络中端到端设计的一篇很好的论文~~~大家可以轻松获得
计算机网络中端到端论点的经典论文 作者:J.H. Saltzer, D.P. Reed and D.D. Clark M.I.T. Laboratory for Computer Science
All function arguments in ECMAScript are passed by value
Finding, Scoring and Explaining Arguments in Bayesian Networks_贝叶斯网络中参数的发现、评分和解释.pdf
网络组成原理经典论文阅读报告,这篇论文提出了一个分布式计算机系统功能连接的端到端设计原则。作者提出,在底层的网络传输系统中进行复杂的工作将会使得整个系统效率低下。不如让底层网络传输只专心进行数据传输,...
AIX64位操作系统服务器,Java6_64.sdk.tar,weblogic10介质,apache-http服务器 承载实例 服务器状态90 apache:133.0.175.90:8080 AIX5.3操作系统,jdk1.6,rpm环境 Adm ...
write a Java program called InputParms.java that accepts 3 arguments in the main method using an array. Iterate through the array looking for your name using a for loop. Display the message "The name ...
JS:arguments
在函数定义时没有形参名称时,向函数传递参数的使用方法。 实例演示了arguments对象的使用方法。
理解Javascript函数形式参数与arguments
Rethinking the design of the Internet:The end to... Clark, M.I.T. Lab for Computer Science 这是Clark后来反思端到端论点的论文,此前他写过关于端到端论点的一篇经典论文:End to End arguments in System Design
Arguments and results by James Noble
A Rulebook for Arguments (3rd Edition)
Python_default_parameters _&_ named_arguments Python分配参数和参数
前端开源库-arguments-extended参数扩展,用于处理参数对象的实用程序
Arguments and Results 参数,资源
Function input arguments always precede output arguments
FC++ is a library for functional programming in C++. Functional programming is a programming paradigm in which functions are treated as regular values. Thus, we can have functions that take other ...
Dimensional reasoning, scale arguments, and similarity rules are introduced at the beginning and are applied throughout.A discussion of Reynolds stress and the kinetic theory of gases provides the ...
Index of current file in argv.Number of arguments in argv.