如何从闭包定义函数
这个问题与我最近问过的问题相关。
如果我重写 (fn [n] (fn [x] (apply * (repeat nx))))
因为
(defn power [n] (fn [x] (apply * (repeat n x))))`
它在这样使用时工作得很好,
((power 2) 16)
我可以用另一种幂代替 2,但是我'我想为平方、立方等创建一个函数。最好的方法是什么?我一直在 REPL 中摆弄它,但到目前为止还没有成功。
This question is related to one I asked recently.
If I rewrite (fn [n] (fn [x] (apply * (repeat n x))))
as
(defn power [n] (fn [x] (apply * (repeat n x))))`
it works just fine when used like this
((power 2) 16)
I can substitute 2 with another power, but I'd like to make a function just for squares, cubed, and so on. What is the best way to do that? I've been fiddling with it in the REPL, but no luck so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用宏完全解决了他的问题,即“我有一个生成闭包的函数,我如何为这些闭包命名?”简单的解决方案是:
如果您真的讨厌多次重复
def
和power
,那么只有到那时才需要使用宏。但是,与使用函数执行此操作的简单性相比,除非您定义的函数至少达到十次方,否则您在即使是最简单的宏上花费的精力也会被浪费。Using a macro for this goes entirely around his question, which was "I have a function that generates closures, how do I give those closures names?" The simple solution is:
If you really truly hate repeating
def
andpower
a few times, then and only then is it time to get macros involved. But the amount of effort you'll spend on even the simplest macro will be wasted unless you're defining functions up to at least the tenth power, compared to the simplicity of doing it with functions.不太确定这是否是您正在寻找的内容,但宏模板可能就是它。以下是我编写代码的方式:
对于更复杂类型的类似任务,您可以编写一个包装
do-template
的宏来对其参数执行一些转换,例如:PS:该宏可能看起来很尴尬如果您刚刚开始使用 Clojure,请不要因此而放弃! ;-)
Not quite sure if this is what you're searching for, but macro templates might be it. Here's how I would write your code:
For more complex type of similar tasks, you could write a macro that wrap
do-template
to perform some transformation on its arguments, e.g.:P.S.: That macro might look awkward if you're just starting with Clojure though, don't give up because of it! ;-)