枚举和Clojure [英] Enums and Clojure

查看:114
本文介绍了枚举和Clojure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java / C世界中,人们经常使用枚举。如果我使用一个使用枚举的Java库,我可以在它们和关键字之间转换,例如使用(。java.lang.Enum valueOf e ...,(aget ^[Ljava。但是在Clojure的世界里,人们是否需要像枚举(一个命名的整数)这样的东西呢?如果没有,怎么样呢?是他们的代码结构化的,他们不需要他们?如果是的,什么是等价的?我觉得我真的要问关于索引(循环),这是很少使用的功能编程(我已经使用映射索引只有一次到目前为止)。

In the Java/C world, people often use enums. If I'm using a Java library which using enums, I can convert between them and keywords, for example, using (. java.lang.Enum valueOf e..., (aget ^"[Ljava.lang.Enum;" (. e (getEnumConstants)) i), and some reflection. But in the Clojure world, do people ever need anything like an enum (a named integer) ? If not, how is their code structured that they don't need them ? If yes, what's the equivalent ? I sense I'm really asking about indices (for looping), which are rarely used in functional programming (I've used map-indexed only once so far).

推荐答案

对于几乎所有的Clojure代码,我看到关键字倾向于使用, / b>它们是名称间隔的,并且具有关键字的所有其他有用的属性,同时更容易编写它们不是确切的站,因为它们比Java枚举更动态(如在动态类型中)

For almost all the Clojure code I have seen keywords tend to be used instead of Enums they are name-spaced and have all the other useful properties of keywords while being much easier to write. They are not an exact standin because they are more dynamic (as in dynamic typing) than Java enums

对于索引和循环,我发现它更习惯地映射一系列关键字:

as for indexing and looping I find it more idiomatic to map over a sequence of keywords:

(map do-stuff [:a :b :c :d] (range)) 

循环遍历枚举中的值,我还没有在Clojure代码中找到一个例子,虽然一个例子很可能存在; - )

than to loop over the values in an enumeration, which I have yet to find an example of in Clojure code, though an example very likely exists ;-)

这篇关于枚举和Clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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