将var或#'应用于Clojure中的函数列表 [英] Applying var or #' to a list of functions in Clojure

查看:115
本文介绍了将var或#'应用于Clojure中的函数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Clojure中读取函数集合的元数据,但var或reader特殊形式不工作,除非他们直接处理该符号。 pre> ;这工作
(var my-fn)

;这不是
(defn val-it [x](var x))
(val-it my-fn)

有什么方法可以让它在另一个函数的范围内工作?

解决方案

resolve 返回与当前命名空间上下文中给定符号对应的Var或类对象。 ns-resolve 允许您指定解析符号的命名空间。

 (解决'my-fn)
; =#'some.ns / my-fn

如果符号无法解析为Var,则返回 nil


I'm trying to read metadata for a collection of functions in Clojure, but the var or reader special forms do not work unless they are directly dealing with the symbol.

; this works
(var my-fn)

; this doesn't
(defn val-it [x] (var x))
(val-it my-fn)

Is there any way to get this to work within the scope of another function?

解决方案

resolve returns the Var or class object corresponding the given symbol in the context of the current namespace. ns-resolve allows you to specify which namespace to resolve the symbol in.

(resolve 'my-fn)
;= #'some.ns/my-fn

If the symbol cannot be resolved to a Var, nil is returned.

这篇关于将var或#'应用于Clojure中的函数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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