本文介绍了clojure类型转换字符串到符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

in clojure我有矢量[myfn1myfn2myfn3]
我如何可以调用名为myfn1的函数...使用该向量中的字符串

myfn1调用绑定到Var myfn1 的函数 code>,您可以这样做:

 ((resolve(symbolmyfn1))... ); ...表示在何处放置任何参数

你需要向你的函数传递任何额外的参数(如果你这样做,你可以修改这个代码),你可以这样做:

 (map#((resolve(symbol%)))[myfn1myfn2myfn3])


b $ b

Eg

  user => (asdfasdfasdf])
(asdf
asdfnil asdf
nil nil)

nil s是打印函数的返回值;请注意 print asdf 后没有换行符> 生成的 asdf 是由 prn 引起的。)


in clojure i have vector ["myfn1" "myfn2" "myfn3"]how can i call functions named "myfn1" ... using strings from that vector

解决方案

To call a function bound to Var myfn1 given the string "myfn1", you could do something like this:

((resolve (symbol "myfn1")) ...) ; ... indicates where to put any arguments

So, given your example vector and assuming that you don't need to pass any additional arguments to your functions (it's straighforward enough to modify this code if you do), you could do the following:

(map #((resolve (symbol %))) ["myfn1" "myfn2" "myfn3"])

E.g.

user=> (map #((resolve (symbol %1)) %2) ["println" "print" "prn"] ["asdf" "asdf" "asdf"])
(asdf
asdfnil "asdf"
nil nil)

(The nils are the return values from the printing functions; note how there's no linebreak after the asdf produced by print and the asdf produces by prn is quoted.)

这篇关于clojure类型转换字符串到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 22:02