作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 clojure 的新手,基本上正在尝试遵循一些教程。我有一个关于 defrecord
的问题。
这是我正在尝试做的事情:
(defrecord somemap [key1 key2 key3 key4])
(defn give-me-map [m1 m2]
(somemap. m1 m2))
在上面的代码中,我希望将 key3
和 key4
作为可选,这样我就不需要每次创建 时都给它们赋值>somemap
对象。
有类似的问题here ,但它与我在这里尝试做的相反。
那么是否可以使用可选字段定义 defrecord
?
最佳答案
当您使用(defrecord T [...])
时,会创建两个工厂函数:->T
和map->T
>.
第一个使用位置参数作为键。第二个将任意映射应用于记录。
您可以将 key3
和 key4
保留在构造函数中并使用 map->somemap
。
(defrecord somemap [key1 key2 key3 key4])
(defn give-me-map
[key1 key2]
(map->somemap {:key1 key1 :key2 key2}))
关于clojure - 使用可选键进行 defrecord,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13391994/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!