简体   繁体   English

Clojure功能检查

[英]Clojure Function check

When i have 3 functions in a program, how do i check a specific function name ? 当我的程序中有3个功能时,如何检查特定的功能名称? I want to know the name of those function for the sake of function selection. 为了功能选择,我想知道这些功能的名称。

Let say linear-kernel function, logistic-kernel function, and non-negative function, when i call the program, one of those function is called and i should to check whether it was linear, logistic or non-negative function, so i can execute another function related with the selected function. 假设线性内核函数,logistic内核函数和非负函数,当我调用程序时,将调用其中一个函数,因此我应该检查它是线性,logistic还是非负函数,因此我可以执行与所选功能相关的另一个功能。

I think doing function selection will save my time from repeating the base code. 我认为进行函数选择将节省重复代码的时间。 But doing function selection maybe is not the best design that i could use in Clojure. 但是进行功能选择可能不是我可以在Clojure中使用的最佳设计。

FYI, at this level, i already use the "meta" keyword to access the function name, but when i create 仅供参考,在此级别上,我已经使用“元”关键字来访问函数名称,但是当我创建时

(defn isKernel [krn]
  (if (= (str (:name (meta #'krn))) "logistic-kernel") 1 0))

The compiler cannot resolve the 'krn' var 编译器无法解析“ krn”变量

In Clojure functions are values, just like the number 4. This is a big part of the underpinnings of much of the language (and functional programming in general). 在Clojure中,函数是值,就像数字4一样。这是大多数语言(和一般的函数式编程)基础的重要组成部分。 Most of the time we store functions in vars though this is not required. 大多数情况下,我们不需要将函数存储在vars中 functions as values don't have names * So rather than checking to see if the name of a passed function matches a known name, it makes more sense to ask " does this function have the same value as the function stored in the var " as @cgrand points out this can be accomplished simply by calling = . 函数作为值没有名称 *因此,与其检查传递的函数的名称是否与已知名称匹配, 不如问“ 该函数的值是否与存储在var中的函数相同 ”更有意义。 @cgrand指出,只需调用=即可完成此操作。

If you are doing this kind of dispatch there is a good change that protocols are a better tool than rolling your own 如果您正在执行这种调度,则存在很大的变化,那就是协议比滚动自己的协议是更好的工具

*they do have names for the purpose of creating recursive function literals though thats not what I'm getting at here. *它们确实具有用于创建递归函数文字的名称,尽管那不是我在这里得到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM