Clojure的隐藏功能 [英] Hidden features of Clojure

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

问题描述

Clojure的哪些不太知名但有用的功能是你自己使用的?



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





解决方案

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

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

列出conj在前面,pop从前面。矢量结合在后面和从后面pop。所以队列有时是你需要的。

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


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:

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

解决方案

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)

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天全站免登陆