Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77b41d6

Browse files
committed
additional corrections to assoc index bounds
1 parent fc0b715 commit 77b41d6

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

02_composite-data/2-07_adding-an-item.asciidoc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ or more items appended to the end:
2424

2525
==== Discussion
2626

27-
Vectors do not support adding new items anywhere aside from the end. If you need to insert an item in the middle, you will have to use a
28-
sequence manipulation function and convert back to a vector (if
29-
necessary) when you're done.
27+
Vectors do not support adding new items anywhere aside from the end. If you need to insert
28+
an item in the middle, you will have to use a sequence manipulation function and convert back
29+
to a vector (if necessary) when you're done.
3030

3131
Since vectors are associative (mapping integer indexes to values), you(((functions, assoc)))
32-
can also use the +assoc+ function with an index one greater than the
33-
current length of the vector to append an item:
32+
can also use the +assoc+ function with an index equal to the current length of the vector
33+
(one greater than the maximum index) to append an item:
3434

3535
[source,clojure]
3636
----
@@ -41,8 +41,7 @@ current length of the vector to append an item:
4141
However, this approach is somewhat more fragile than +conj+. If the
4242
index you provide is too small, you might simply "overwrite" an
4343
earlier value in the vector; and if it's greater than the vector's
44-
current length (one greater than the maximum index),
45-
it will throw an +IndexOutOfBoundsException+.
44+
current length, it will throw an +IndexOutOfBoundsException+.
4645

4746
Still, this technique is worth remembering. If you have code that is
4847
+assoc+-ing to a vector already, you can use this technique to produce

02_composite-data/2-10_set-item-at-index.asciidoc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ hood.((("Clojure", "clojure.lang.Associative")))
3737
Unlike with maps, however, the keys used when using +assoc+ on a vector
3838
must be integer indexes within the range of the vector. Attempting to
3939
use a noninteger key will cause an +IllegalArgumentException+, and
40-
attempting to +assoc+ an index greater than one more than the size of
41-
the vector will throw an +IndexOutOfBoundsException+.(((exceptions/errors, IllegalArgumentException)))(((exceptions/errors, IndexOutOfBoundsException)))
40+
attempting to +assoc+ an index greater than the size of the vector will throw
41+
an +IndexOutOfBoundsException+.(((exceptions/errors, IllegalArgumentException)))(((exceptions/errors, IndexOutOfBoundsException)))
4242

43-
Note that it _is_ possible to +assoc+ to an index equal to the
44-
current size of the vector (one greater than the maximum index). This will have
45-
the result of appending the item to the end
46-
(see <<sec_adding_to_a_vector>>).(((range="endofrange", startref="ix_CDvect")))
43+
Note that it _is_ possible to +assoc+ to an index equal to the current size
44+
of the vector (one greater than the maximum index). This will have
45+
the result of appending the item to the end (see <<sec_adding_to_a_vector>>).(((range="endofrange", startref="ix_CDvect")))
4746

4847
==== See Also
4948

0 commit comments

Comments
 (0)