Clojure - 将列表转换为Java数组 [英] Clojure - convert list into Java array

查看:171
本文介绍了Clojure - 将列表转换为Java数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何惯用的方式将Clojure列表转换为Java数组,除了首先将其转换为向量并使用 into-array (意味着 array(vec my-list)),因为我不想要额外的开销)?

Is there any idiomatic way of converting Clojure list into Java array, other than first converting it to vector and using into-array (means, something other than (into-array (vec my-list)), as I don't want additional overhead)?

推荐答案

>你的问题似乎是基于一个错误的前提。 into-array不需要取一个向量,它需要一个seqable值。文档( http://clojuredocs.org/clojure_core/clojure.core/into-array )包含在非向量序列上使用into-array的示例:

Your question seems to be based on a false premise. into-array does not need to take a vector, it takes a seqable value. The documentation ( http://clojuredocs.org/clojure_core/clojure.core/into-array ) contains examples of using into-array on a non-vector sequence:

user=> (into-array (range 4))
#<Integer[] [Ljava.lang.Integer;@63d6dc46>

user=> (type (range 4))
clojure.lang.LazySeq

user=> (doc range)
-------------------------
clojure.core/range
([] [end] [start end] [start end step])
  Returns a lazy seq of nums from start (inclusive) to end
  (exclusive), by step, where start defaults to 0, step to 1, and end
  to infinity.

在列表中调用它也很方便:

Calling it on a list works just as well:

user=> (into-array (list 1 2 3))
#<Long[] [Ljava.lang.Long;@138297fe>

这篇关于Clojure - 将列表转换为Java数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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