本文介绍了Clojure:变换Varargs但保持他们Varargs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Clojure工作了一个小宠物项目。我有一个函数,我传递varargs:

I am working on a little pet project in Clojure. I have a function that I pass varargs:

(defn foor [bar & args]
  (let new-args (custom-transform args)]
    (do-something new-args))))))


$ b b

但是, do-something 期望varargs,而不是列表对象。在 custom-transform

But, do-something is expecting varargs, not a list object. In custom-transform

(defn custom-transform [& args]
    (if vars vars nil))



如何保留vararg

How do I preserve the "vararg"-iness of my args after applying a transformation?

推荐答案

我认为apply应该做的诀窍:

I think apply should do the trick:

(apply do-something new-args)


$ b b

您的问题不是varargs特有的。你想做的是调用 do-something ,传递数组的元素作为函数的参数。这恰恰是应用的确切原因。

Your problem isn't specific to varargs. What you want to do is to call do-something, passing the elements of an array as the arguments to the function. This happens to be precisely what apply does.

这篇关于Clojure:变换Varargs但保持他们Varargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 10:25