什么类型是函数? [英] What type is a function?

查看:121
本文介绍了什么类型是函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们尝试对类型函数的一些调用:

let's try some calls to the "type" function :

user=> (type 10)
java.lang.Integer

user=> (type 10.0)
java.lang.Double

user=> (type :keyword?)
clojure.lang.Keyword

user=> (type #(str "wonder" "what" "this" "is"))
user$eval7$fn__8

A)这是什么意思是user $ eval7 $ fn__8?
B)以及什么类型是函数?

A) what does this mean "user$eval7$fn__8" ? B) and what type is a function ?

type的源是:

user=> (source type)
(defn type
  "Returns the :type metadata of x, or its Class if none"
  {:added "1.0"}
  [x]
  (or (:type (meta x)) (class x)))
nil

因此,函数需要具有元数据的特定部分或者是一个类

so a function needs to have a specific part of meta data or be a class

检查匿名函数的元产生nada:

checking the meta of an anonymous function yields nada :

user=> (meta #(str "wonder" "what" "this" "is"))
nil

尝试不同的方法:

user=> (defn woot [] (str "wonder" "what" "this" "is"))
#'user/woot
user=> (meta woot)
{:ns #<Namespace user>, :name woot}

C)似乎有一些元,但我想这是符号woot的元,是吗?

C) seems there is some meta but i figured this is the meta of the symbol "woot", right ?

一半的或:

user=> (class #(str "wonder" "what" "this" "is"))
user$eval31$fn__32

user=> (class woot)
user$woot

这些是什么:user $ eval31 $ fn__32 和user $ woot,它们来自哪里?

what are these : "user$eval31$fn__32" and "user$woot" and where do they come from ?

检查类函数,结果如下:

checking out the "class" function yields:

user=> (source class)
(defn ^Class class
  "Returns the Class of x"
  {:added "1.0"}
  [^Object x] (if (nil? x) x (. x (getClass))))
nil

user=> (.getClass #(str "wonder" "what" "this" "is"))
user$eval38$fn__39

user=> (.getClass woot)
user$woot


D)这是一个哈希码:eval38 $ fn__39?
E)是这个符号:woot?

i don't get it. D) is this a hashcode : eval38$fn__39 ? E) is this a symbol : woot ?

F)有类型?

推荐答案

Clojure是建立在JVM上的。

Clojure is built on the JVM.

JVM不支持开箱即用的一级函数或lambdas。每个Clojure函数一旦被编译,就从JVM的角度成为它自己的匿名类。

The JVM doesn't support first-class functions, or lambdas, out of the box. Every Clojure function, once it is compiled, becomes its own anonymous class from the perspective of the JVM. Each function is, technically, it's own type.

这个类会变成实现 IFn,但是当你检索它的类型时,它会给你每次不同的匿名类的名称。

The class it becomes implements IFn, but when you retrieve it's type, it gives you the name of the anonymous class which is different every time.

这篇关于什么类型是函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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