函数重载使用不同数量的参数 [英] functions overloaded with different number of arguments

查看:211
本文介绍了函数重载使用不同数量的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多Clojure的函数接受各种数量的参数,我经常受了一点文档困惑,以及如何它涉及到我应该使用功能的方式。

A lot of the Clojure functions accept various number of arguments and I'm often a bit confused by the documentation and how it relates to the way I should be using the function.

例如(DOC部分)返回这样的:

([f arg1] [f arg1 arg2] [f arg1 arg2 arg3] [f arg1 arg2 arg3 & more])

我的问题是不是专讲的部分的,但...

为什么到的 ARG1参数3&安培;更多的而不是 ARG1和放大器;更多 ARG1 ARG2&安培;更多 ARG1参数3 ARG4 arg5 arg6 arg7 arg8&安培;更多的?

Why up to arg1 arg2 arg3 & more and not arg1 & more or arg1 arg2 & more or arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 & more ?

我在开玩笑的最后一个,但是这是一个非常现实的问题:什么决定了argX一需要的&放前放;更多的?

I'm being facetious with the last one but this is a very real question: what determines how many "argX" one needs to put before the & more?

推荐答案

以下的答案必须是我的一个猜测:一看实施部分告诉我们:

The following answer is a guess on my part: A look at the implementation of partial show us:

(defn partial
  "Takes a function f and fewer than the normal arguments to f, and
  returns a fn that takes a variable number of additional args. When
  called, the returned function calls f with args + additional args."
  {:added "1.0"
   :static true}
  ([f arg1]
   (fn [& args] (apply f arg1 args)))
  ([f arg1 arg2]
   (fn [& args] (apply f arg1 arg2 args)))
  ([f arg1 arg2 arg3]
   (fn [& args] (apply f arg1 arg2 arg3 args)))
  ([f arg1 arg2 arg3 & more]
   (fn [& args] (apply f arg1 arg2 arg3 (concat more args)))))

正如你所看到的,局部的每一个电话是做同样的事情 - 即返回它接受一些指定参数和调用的函数适用与输入ARGS输入功能新ARGS。因此,这确实可以被写成 ARG1和放大器;更多。别急,让我们来看看适用的实施,以及:

As you can see, every call to partial is doing the same thing - namely, returning a function which accepts some args and calls apply on the input function with input args and new args. So this could indeed have been written as arg1 & more. But wait, lets look at the implementation of apply as well:

(defn apply
 "Applies fn f to the argument list formed by prepending intervening arguments to args."
 {:added "1.0"
  :static true}
 ([^clojure.lang.IFn f args]
    (. f (applyTo (seq args))))
 ([^clojure.lang.IFn f x args]
    (. f (applyTo (list* x args))))
 ([^clojure.lang.IFn f x y args]
    (. f (applyTo (list* x y args))))
 ([^clojure.lang.IFn f x y z args]
    (. f (applyTo (list* x y z args))))
 ([^clojure.lang.IFn f a b c d & args]
    (. f (applyTo (cons a (cons b (cons c (cons d (spread args)))))))))

应用是一个核心功能,并给出一个不同的数目的参数,当它被执行不同。这是应用性能方面的原因的优化。这是暴露的部分(和其他类似功能)不同arities的原因,是因为code的内部执行对于不同arities。

Apply is a core function, and it is executes differently when given a different number of arguments. This is an optimization to apply for performance reasons. This is the reason for exposing different arities of partial (and other such functions), because the internal execution of the code differs for different arities.

我假设Clojure的/核心团队认为,暴露部分的元数超越ARG1参数3&安培;更多(即写ARG1参数3 ARG4及更多)也不会审美,所以他们决定停止在3 ARGS&安培;更多。

I'm assuming that the clojure/core team thought that exposing the arity of partial beyond arg1 arg2 arg3 & more (ie writing arg1 arg2 arg3 arg4 & more) would not be aesthetic, so they decided to stop at 3 args & more.

这篇关于函数重载使用不同数量的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆