Clojure 的隐藏特性 [英] Hidden features of Clojure

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

问题描述

您发现自己在使用 Clojure 哪些鲜为人知但有用的功能?随意分享小技巧和习惯用法,但尽量将自己限制在 Core 和 Contrib.

Which lesser-known but useful features of Clojure do you find yourselves using? Feel free to share little tricks and idioms, but try to restrict yourselves to Core and Contrib.

我在回答这些类似问题时发现了一些非常有趣的信息:

I found some really interesting information in answers to these similar questions:

其他语言还有更多隐藏功能"问题,所以我认为 Clojure 也有一个问题会很好.

There are many more "Hidden feature" questions for other languages, so I thought it would be nice to have one for Clojure, too.

推荐答案

Clojure 有一个不可变的、持久的队列数据类型 PersistentQueue,但它(还没有?)没有文字阅读器语法或 Clojure 包装器函数,所以你必须通过 Java 调用创建一个.队列连接(推)到后面并从前面弹出,性能良好.

Clojure has an immutable, persistent queue datatype, PersistentQueue, but it doesn't (yet?) have literal reader syntax or Clojure wrapper functions, so you have to create one via a Java call. Queues conj (push) onto the rear and pop from the front with good performance.

user> (-> (clojure.lang.PersistentQueue/EMPTY)
          (conj 1 2 3)
          pop)
(2 3)

将 conj 列在前面并从前面弹出.矢量连接到后方并从后方弹出.所以队列有时正是您所需要的.

Lists conj onto the front and pop from the front. Vectors conj onto the rear and pop from the rear. So queues are sometimes exactly what you need.

user> (-> ()
          (conj 1 2 3)
          pop)
(2 1)
user> (-> []
          (conj 1 2 3)
          pop)
[1 2]

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

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