Clojure: How to replace an element in a nested list? -


I have this intense nested list (list of lists) and I want to change a single arbitrary element in the list. how can I do this ? (The underlying replacement can replace many events, whereas I only need to change one element.)

< P> As everyone has already said, the use of lists is not really a good idea if you need to talk about this type of thing to the Random Access Vector. assoc-in does it efficiently, with lists you can not return to the sub-slips downward and most of them can return to the top with their changed versions.

Although this code will do so, though inefficiently and unconsciously. Credit from dermatitis:

  (replacement-in-list [coll nx] (contact (nn call) (list x) (nthnext coll (inc n))) () in sub-list [ Cole NS X] (if the CEC ns) (let sublist (nth coll)) (per-in-list call (first ns) (replacement-in-cibilist sublist (rest ns) x)))) )) X))  

Usage:

  User & gt; (Def x '(0 1 2 (0 1 (0) 2 (0 1 2)) #' User / X User & gt; (Replacement-in-Sibylist X [3 2 0]: Foo) ( 0 1 2 (0 (1 (Fu 1 2) 3 4 (0 1 2))) User> (Replacement-in-Sibylist X [3 2]: Foo) (0 1 2 (0 1: Foo 3 4 (0 (2: 2) (0): (0): (0): (0): (0) : Bar) 2)))  

If you give any n more than the length of a sublist then you will get IndexOutOfBoundsException This tail It's not idiotic even. Because good closing code is eliminated from using lists for everyone, it is terrible. Before I use it, maybe I will use accurate Java arrays, I think you get this idea.

Edit

Why the lists are worse than the vectors in this case:

  (time (let [x] (0 1 2 0 1 (0 1 2) 3 4 (0 1 2)]; '(Ditems [_ 1e6] (Replacement-in-Siblast X [3 2]]: Foo))) "Elapsed time: 5201.110134 msecs "Zero User" (Time ([x [0 1 2 [0 1 [0 1 2] 3 4 [0 1 2]]]] (Ditems [_ 1e6] (Exchange x [3 2 0]: AF))) "Time has passed It has been: 2925.318122 MSECE "zero  

You do not even have to write assoc-in , it already exists. assoc-in look at the implementation for some time; It is simple and simple (compared to the list version) to give efficient and easy random access through the pointer to vector, through get .

You do not even have to bid like vectors, you have to quote the lists, the emphasis of the lists in the closure means "I'm calling a function or a macro".

Vector (and map, set etc.) seq s You can use a vector in a list-type way transparently, why not use vectors and both Best of the world?

Vectors also stand visually, due to the widespread use of the [] and {} , the closure code is more than a large leg of the legs compared to other lisps Less than Blobs. Some people find it annoying, I think these things make it easy to read. (My Editor Syntax-Highlight () , [] and {} which help differently.)

< P> In some instances, I use a list for data:

  1. If I have an ordered data structure that needs to be increased from the front, then never give me random access
  2. >

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

jquery - SimpleModal Confirm fails to submit form -