4

I have the a list in clojure, and (due to the underlying java library) must modify the list (using the iterator's remove method). Is there a more elegant way to get this effect in closure than writing a destructive equivalent of (map fn seq)?

1
  • What are you using these lists for? Do they have to be native Java lists? How often are they mutated? Commented Jul 26, 2009 at 1:07

1 Answer 1

4

Clojure lists are immutable so if you need a mutable list it's always possible to use one that Java provides.

For example:

user=> (import java.util.LinkedList)                  
java.util.LinkedList
user=> (def a (list 3 6 1 3))           
#'user/a
user=> (def b (java.util.LinkedList. a))
#'user/b
user=> b
#<LinkedList [3, 6, 1, 3]>
user=> (.remove b 6)
true
user=> b
#<LinkedList [3, 1, 3]>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.