Questions tagged [mutable]

A mutable can be modified after it is created.

A mutable can be modified after it is created. It is a keyword in functional programming opposite to immutable.

Related tags:

1184 questions
11
votes
1 answer

What is the difference between matching a mutable Option reference in "if let Some(ref mut x) = option" and in "if let Some(x) = option.as_mut()"?

Background Consider a toy problem in which I have a Node struct that represents nodes of linked lists and that I want to create a function that builds a list with values from 1 to 9. The following code works as expected: struct Node { val: i32, …
luca_moller
  • 156
  • 7
11
votes
5 answers

How to solve ImportError: dlopen(): Symbol not found:.... Expected in: flat namespace

Can anyone help me solve this issue? ImportError: dlopen(/Users/......./venv/lib/python3.6/site-packages/recordclass/mutabletuple.cpython-36m-darwin.so, 2): Symbol not found: __PyEval_GetBuiltinId Referenced from:…
Oliver Robie
  • 818
  • 2
  • 11
  • 30
11
votes
2 answers

How to convert immutable Seq to mutable seq with until loop

I'm trying to return a mutable Sequence with an until loop, but i have an immutable seq in return of (0 until nbGenomes) : def generateRandomGenome(nbGenomes:Int): IndexedSeq[GenomeDouble]={ return ((0 until nbGenomes toSeq).map{e =>…
reyman64
  • 523
  • 4
  • 34
  • 73
11
votes
1 answer

How to get functools.lru_cache to return new instances?

I use Python's lru_cache on a function which returns a mutable object, like so: import functools @functools.lru_cache() def f(): x = [0, 1, 2] # Stand-in for some long computation return x If I call this function, mutate the result and…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
11
votes
3 answers

How do I pass a mutable list to a bundle?

I want to add a mutable list to a bundle, but there doesn't seem to be a way to accomplish this. var bundle = Bundle() bundle.put...????("LIST", fbModel.recipeArray) You can use putString and so on, but there doesn't seem to be a…
tore
  • 619
  • 2
  • 9
  • 23
11
votes
4 answers

Is modifying a value type from within a using statement undefined behavior?

This one's really an offshoot of this question, but I think it deserves its own answer. According to section 15.13 of the ECMA-334 (on the using statement, below referred to as resource-acquisition): Local variables declared in a …
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
11
votes
2 answers

How to mutate a list with a function in python?

Here's a pseudocode I've written describing my problem:- func(s): #returns a value of s x = a list of strings print func(x) print x #these two should give the SAME output When I print the value of x in the end, I want it to be the one returned…
Ritvik Sharma
  • 113
  • 1
  • 1
  • 4
11
votes
1 answer

PickleType with Mutable Tracking in SqlAlchemy

I have a project where I would like to store a large structure (nested objects) in a relational db (Postgres). It's part of a larger structure and I don't really care about the serialization format - I'm happy for it to be a blob in a column - I'd…
Aidan Kane
  • 3,856
  • 2
  • 25
  • 28
11
votes
5 answers

Generating sublists using multiplication ( * ) unexpected behavior

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…
A.Wan
  • 1,818
  • 3
  • 21
  • 34
10
votes
3 answers

What is the best approach to convert immutable objects to mutable objects (recursive)?

Specifically, this problem has come to me when I make a request with AFNeworking with JSONkit and receive a (id)JSON with several arrays and dictionaries nested. If I don't want to modify the data, I don't have any problem: self.myNSArray = [JSON…
martinezdelariva
  • 5,011
  • 2
  • 25
  • 30
10
votes
3 answers

Haskell real-time update and lookup performance

I am writing a game-playing ai (aichallenge.org - Ants), which requires a lot of updating of, and referring to data-structures. I have tried both Arrays and Maps, but the basic problem seems to be that every update creates a new value, which makes…
Greg O'Keefe
  • 101
  • 7
10
votes
2 answers

Scala: Contains in mutable and immutable sets

I've discovered a strange behavior for mutable sets which I cannot understand: I have a object which I want to add to a set. The equals method for the class is overridden. When I add two different objects to the set, which produces the same output…
Stefan
  • 111
  • 5
10
votes
1 answer

In Rust, how do I create a mutable iterator?

I'm having difficulty with lifetimes when trying to create a mutable iterator in safe Rust. Here is what I have reduced my problem to: struct DataStruct { inner: Box<[T]>, } pub struct IterMut<'a, T> { obj: &'a mut DataStruct, …
Barnaby Dalton
  • 158
  • 1
  • 1
  • 8
10
votes
4 answers

Caching expensive data in C++ - function-scoped statics vs mutable member variables

I've got a relatively expensive data-fetching operation that I want to cache the results of. This operation is called from const methods, roughly like this: double AdjustData(double d, int key) const { double factor =…
Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
10
votes
1 answer

When an immutable reference to a mutable reference to a value outside the scope is returned, why is the mutable reference dropped when the scope ends?

fn main() { // block1: fails { let mut m = 10; let n = { let b = &&mut m; &**b // just returning b fails }; println!("{:?}", n); } // block2: passes { let mut m =…
soupybionics
  • 4,200
  • 6
  • 31
  • 43