调用Clojure高阶函数 [英] Calling Clojure higher-order functions

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

问题描述

如果我定义一个函数,返回一个这样的函数:

 (defn add-n 
[n ]
(fn [x](+ xn)))

结果为符号:

 (def add-1(add-n 1))



并将其命名为

  -1 41)
; => 42

如何调用(add-n 1) / code>,而不将它分配给一个新的符号?以下产生此输出:

 (println(add-n 1))
#< user $ add_n $ fn__33 user $ add_n $ fn__33 @ e9ac0f5>
nil

#< user $ add_n $ fn__33用户

解决方案

Easy:$ add_n $ fn__33 @ e9ac0f5> 是对生成函数的内部引用。

 (println((add-n 1)41))

你看到的输出是一个函数定义。将它放在圆括号之间并添加参数就足以调用它。


If I define a function that returns a function like this:

(defn add-n
  [n]
  (fn [x] (+ x n)))

I can then assign the result to a symbol:

(def add-1 (add-n 1))

and call it:

(add-1 41)
;=> 42

How do I call the result of (add-n 1) without assigning it to a new symbol? The following produces this output:

(println (add-n 1))
#<user$add_n$fn__33 user$add_n$fn__33@e9ac0f5>
nil

The #<user$add_n$fn__33 user$add_n$fn__33@e9ac0f5> is an internal reference to the generated function.

解决方案

Easy:

(println ((add-n 1) 41))

The output you saw is a function definition. Putting it between round brackets and adding a parameter is enough to call it.

这篇关于调用Clojure高阶函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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