I would like array1 to remain unchanged. It seems a little inefficient
to clone array1 every time some_method is called. Is there a more
elegant solution?
I would like array1 to remain unchanged. It seems a little inefficient
to clone array1 every time some_method is called. Is there a more
elegant solution?
Couldn’t you do something like this?
arr1 = (“a”…“j”).to_a
i = 3
p arr1[0…i]+arr1[i+1…-1] #> [“a”, “b”, “c”, “e”, “f”, “g”, “h”,
“i”, “j”]
I would like array1 to remain unchanged. It seems a little inefficient
to clone array1 every time some_method is called. Â Is there a more
elegant solution?
Could you clarify what you’re trying to do? What you’ve shown is a
method that takes an array and then returns a slightly modified
version. If you don’t want the original to be modified, what’s your
other option?
Are you aware that array duplication is a ‘shallow’ operation? All
you’re doing is (efficiently) copying a list of references to objects,
not the objects themselves. As lith ≈wrote, avoid premature
optimization. You may be spending your time and ours on a complete non-
issue.
On Sun, 08 Nov 2009 19:40:08 +0900, Timo J. wrote:
end
I would like array1 to remain unchanged. It seems a little inefficient
to clone array1 every time some_method is called. Is there a more
elegant solution?
If you’re comfortable with the idea of using completely immutable lists,
and not changing any of the elements of the list, an implementation of
efficiently catenable lists (such as that described in “Purely
Functional
Data Structures”) might be in order.