【问题标题】:How do I write/perform a type operations in Clojure?如何在 Clojure 中编写/执行类型操作?
【发布时间】:2009-07-23 19:21:54
【问题描述】:

我很难在 Clojure 中编写类型检查。请帮帮我。

  1. 如何编写/执行“字符”?在 Clojure 中?
  2. 如何编写/执行“原子”?在 Clojure 中?

  3. 我如何知道 Clojure 中的项目是什么类型?
    我可以写(type? an-item)吗?

【问题讨论】:

    标签: clojure


    【解决方案1】:
    user> (instance? Character \c)
    true
    

    Character 这里是java.lang.Character

    我认为atom? 的传统定义是“nil,或者任何不是对的东西”,但这在 Clojure 中是没有意义的,因为 Clojure 不使用 cons 单元来处理所有事情。我们还有向量、哈希图和集合等。Clojure 中的一种可能性是:

    (defn atom? [x] (not (coll? x)))
    

    【讨论】:

      【解决方案2】:

      Clojure 中的典型类型问题归结为询问“这是什么类,或者什么原始包装类可以包含它”。这来自 Clojure 对 java 的一流处理。 Clojure 使用 java 类而不是引入它自己的类型系统,这需要很多或包装器来从 java 来回转换。

      1. (= Character (class \a))
      2. (symbol? 'asdf) ; not quite what you want but close. seehttp://clojure.org/reader
      3. (= String (class "asdf"))

      【讨论】:

      • Brians anser 使用实例?比使用 ='s 更好
      猜你喜欢
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2020-02-23
      • 2020-06-11
      相关资源
      最近更新 更多