Java 的 switch 在 Clojure 中等效吗?

发布于 2024-12-19 06:56:55 字数 119 浏览 6 评论 0原文

Clojure 中是否有与 Java 的 switch 结构等效的东西?如果是,那是什么?如果不是,我们是否必须使用 if else 梯子来实现?

Is there an equivalent for Java's switch construct in Clojure? If yes, what is it? If no, do we have to use if else ladder to achieve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆梦 2024-12-26 06:56:55

case 是一个不错的选择,正如 Jan

cond 在许多相关情况下也非常有用,特别是如果您想在评估一系列不同条件表达式的基础上进行切换,例如

(defn account-message [balance]
  (cond 
    (< balance 0) "Overdrawn!"
    (< balance 100) "Low balance"
    (> balance 1000000) "Rich as creosote"
    :else "Good balance"))

注意 cond 的结果是由第一个匹配的表达式决定的,所以负余额会显示“Overdrawn!”即使它也匹配低余额情况。

[我已经编辑了代码 - 删除了末尾的额外括号以使其正常工作]

case is a good option as pointed out by Jan

cond is also very useful in many related circumstances, particularly if you want to switch on the basis of evaluating a range of different conditional expressions, e.g.

(defn account-message [balance]
  (cond 
    (< balance 0) "Overdrawn!"
    (< balance 100) "Low balance"
    (> balance 1000000) "Rich as creosote"
    :else "Good balance"))

Note that the result of cond is determined by the first matching expression, so a negative balance will display "Overdrawn!" even though it also matches the low balance case.

[I have edited the code - removed the extra bracket at the end to make it work]

弄潮 2024-12-26 06:56:55

尝试使用 case 宏:

(case (+ 2 3)
  6 "error"
  5 "ok")

或使用默认值

(case (+ 2 3)
  5 "ok"
  "error")

请记住,根据文档

不评估测试常数。它们必须是编译时文字,并且不需要加引号。 (...)

请参阅 ClojureDocs 查看更多示例。

Try the case macro:

(case (+ 2 3)
  6 "error"
  5 "ok")

or with default value

(case (+ 2 3)
  5 "ok"
  "error")

Remember that according to the documentation

The test-constants are not evaluated. They must be compile-time literals, and need not be quoted. (...)

See more examples at ClojureDocs.

哽咽笑 2024-12-26 06:56:55

虽然 @Jan 和 @mikera 建议使用 大小写cond (我可以添加 condp 到列表中?)从功能性角度来看是合理的,尽管case 的限制(例如,测试值只能是编译时常量;默认返回值是强制的),与 switch 的限制相同,但存在一些细微的差别:

  • case 不能与 Java Enum 常量一起使用;

  • case 的调度基于哈希 AFAIK,这使得它在性能方面可与哈希图相媲美; switch 速度更快;

  • 你不能使用case,这意味着你必须使用其他选项(带有值集的condp?)来镜像switch的行为。


[1] 不像

Though @Jan and @mikera suggestions to use case or cond (may I add condp to the list?) are sound from a functional¹ standpoint and though case 's limitations (e.g. test values can only be compile-time constants ; a default return value is mandatory) mirror those of switch there are some subtle differences:

  • case cannot be used with Java Enum constants ;

  • case 's dispatch is based on hashing AFAIK which makes it comparable to hashmaps in terms of performance ; switch is way faster ;

  • you cannot fall-through with case, which means that you must use other options (condp with value sets ?) to mirror switch 's behaviour.


[¹] not functional as in , functional as in fulfilling a function, serving a purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文