Is there a Java Clojure or Scala version of a constant immutable vector?

That is, constant, but data exchange with effective O (1) indexing.

+4
source share
4 answers

Karl Krukov extracted the clojure data structures into a stand-alone library so you can use it without introducing the whole clojure into your project. There is also pcollections , which implements TreePVector (with a logarithmic time search).

+7
source

I created a library of persistent data structures for Java a couple of years ago that might fit the bill:

https://github.com/mikera/mikera/tree/master/src/main/java/mikera/persistent

They are somewhat similar to Clojure's data structures, but with a lot of Java flavor:

  • Make full use of generics
  • Support for all the Java collection interfaces you expect
  • Include some convenient specialized types (e.g. RepeatList for repeated inputs of the same value)
+3
source

Remember that Clojure, after all, is just Java. That way you can just put the Clojure jar in your classpath and use its classes.

For example, Clojure vectors are instances of clojure.lang.PersistentVector , maps are instances of clojure.lang.PersistentArrayMap and lists, instances of clojure.lang.PersistentList .

I have not tried this in anger myself, but it is probably the line I would take if I wanted to use persistent data structures in Java.

Something like this might start you up:

 import clojure.lang.PersistentVector; ... ArrayList list = ... PersistentVector myVector = PersistentVector.create(list); // from here on, using myVector takes advantages of its persistent nature. 

Obviously, these classes were built using the Clojure API, and not ease of use with Java - but this is certainly possible.

+1
source

Here is the Scala port -> Java: https://github.com/andrewoma/dexx

0
source

All Articles