mutable

Generating sublists using multiplication ( * ) unexpected behavior [duplicate]

本小妞迷上赌 提交于 2019-12-27 11:26:31
问题 This question already has answers here : List of lists changes reflected across sublists unexpectedly (13 answers) Nested List Indices [duplicate] (2 answers) Closed 6 years ago . I'm sure this has been answered somewhere but I wasn't sure how to describe it. Let's say I want to create a list containing 3 empty lists, like so: lst = [[], [], []] I thought I was being all clever by doing this: lst = [[]] * 3 But I discovered, after debugging some weird behavior, that this caused an append

Python 2.7 - clean syntax for lvalue modification

梦想的初衷 提交于 2019-12-25 19:04:42
问题 It is very common to have struct-like types that are not expected to be modified by distant copyholders. A string is a basic example, but that's an easy case because it's excusably immutable -- Python is unusual in even allowing things like method calls on literal strings. The problem is that (in most languages) we frequently have things like, say an (x,y) Point class. We occasionally want to change x and y independently. I.e., from a usage perspective, a Point LVALUE should be mutable (even

Attempt to mutate immutable object error

梦想的初衷 提交于 2019-12-25 12:11:28
问题 My code crashes at this line: [(NSMutableString *)string replaceCharactersInRange:range withString:@""]; with the error attempt to mutate immutable object. How is this happening and how do i fix it? 回答1: The string isn't mutable, casting is not magic, it wouldn't turn it into a mutable string. You should do it on a mutable copy: NSMutableString* mutableString= [string mutableCopy]; [mutableString replaceCharactersInRange:range withString:@""]; 回答2: You are typecasting an immutable string and

Attempt to mutate immutable object error

一世执手 提交于 2019-12-25 12:10:05
问题 My code crashes at this line: [(NSMutableString *)string replaceCharactersInRange:range withString:@""]; with the error attempt to mutate immutable object. How is this happening and how do i fix it? 回答1: The string isn't mutable, casting is not magic, it wouldn't turn it into a mutable string. You should do it on a mutable copy: NSMutableString* mutableString= [string mutableCopy]; [mutableString replaceCharactersInRange:range withString:@""]; 回答2: You are typecasting an immutable string and

How to extend mutable Map in Scala

不想你离开。 提交于 2019-12-24 15:34:54
问题 I would like to extend mutable Map in Scala, because I need some special behaviour when adding a new tuple. I have the following package my.package import collection.mutable.Map class Unit[T1,T2](map: Map[T1,T2]) extends Map[T1,T2]{ override def get(key: T1): Option[T2] = super.get(key) override def iterator: Iterator[(T1, T2)] = super.iterator override def -=(key: T1): Map[T1, T2] = super.-(key) override def +=[T3 >: T2] (kv: (T1, T3)): Map[T1, T3] = super.+[T3](kv) } The problem is with the

Problem creating N*N*N list in Python

天大地大妈咪最大 提交于 2019-12-24 04:09:48
问题 I'm trying to create a 3-dimensional N N N list in Python, like such: n=3 l = [[[0,]*n]*n]*n Unfortunately, this does not seem to properly "clone" the list, as I thought it would: >>> l [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]] >>> l[0][0][0]=1 >>> l [[[1, 0, 0], [1, 0, 0], [1, 0, 0]], [[1, 0, 0], [1, 0, 0], [1, 0, 0]], [[1, 0, 0], [1, 0, 0], [1, 0, 0]]] What am I doing wrong here? 回答1: The problem is that * n does a shallow copy

Mutable borrow in loop [duplicate]

本小妞迷上赌 提交于 2019-12-23 12:00:51
问题 This question already has answers here : Linking the lifetimes of self and a reference in method (1 answer) Cannot borrow as mutable more than once at a time in one code - but can in another very similar (2 answers) Closed 2 years ago . I am trying to get a mutable borrow inside a loop, and I cannot get it to work. I've tried all the possible guards, raw pointers, everything. struct Test<'a> { a: &'a str, } impl<'a> Test<'a> { pub fn new() -> Self { Test { a: &mut "test" } } pub fn dostuff(&

Modifying an object during iteration [duplicate]

南笙酒味 提交于 2019-12-23 03:31:22
问题 This question already has an answer here : How to modify/partially remove a range from a BTreeMap? (1 answer) Closed 3 years ago . I'm trying to translate some simple data structures I use in C++ over to Rust, starting with an interval tree, but I don't understand how to modify my underlying data structure (here an std::collections::BTreeSet ) during iteration - essentially so I can merge overlapping entries as they appear. If I use the standard idiom for iterating over a collection, I get

Modifying an object during iteration [duplicate]

。_饼干妹妹 提交于 2019-12-23 03:31:07
问题 This question already has an answer here : How to modify/partially remove a range from a BTreeMap? (1 answer) Closed 3 years ago . I'm trying to translate some simple data structures I use in C++ over to Rust, starting with an interval tree, but I don't understand how to modify my underlying data structure (here an std::collections::BTreeSet ) during iteration - essentially so I can merge overlapping entries as they appear. If I use the standard idiom for iterating over a collection, I get

Performance of MutableBigInteger

北战南征 提交于 2019-12-23 03:28:55
问题 I tried calculating the sum of the digits of square root of integers below a particular input with large precison (upto 10000) using BigInteger. public class SquareRootHackerRankJarvis { static BigInteger limit; static BigInteger a; static BigInteger b; private static BigInteger squareroot(int n, BigInteger ten, BigInteger hundred, BigInteger five) { a = BigInteger.valueOf(n * 5); b = BigInteger.valueOf(5); while (b.compareTo(limit) == -1) { if (a.compareTo(b) != -1) { a = a.subtract(b); b =