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
92
votes
7 answers

Immutable/Mutable Collections in Swift

I was referring to Apple's Swift programming guide for understanding creation of Mutable/ immutable objects(Array, Dictionary, Sets, Data) in Swift language. But I could't understand how to create a immutable collections in Swift. I would like to…
Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50
89
votes
7 answers

What is the syntax for adding an element to a scala.collection.mutable.Map?

What is the syntax for adding an element to a scala.collection.mutable.Map ? Here are some failed attempts: val map = scala.collection.mutable.Map map("mykey") = "myval" map += "mykey" -> "myval" map.put("mykey","myval")
Koala3
  • 2,203
  • 4
  • 20
  • 15
84
votes
2 answers

Should mutexes be mutable?

Not sure if this is a style question, or something that has a hard rule... If I want to keep the public method interface as const as possible, but make the object thread safe, should I use mutable mutexes? In general, is this good style, or should a…
Marcin
  • 12,245
  • 9
  • 42
  • 49
83
votes
4 answers

Converting mutable to immutable map

private[this]object MMMap extends HashMap[A, Set[B]] with MultiMap[A, B] How convert it to immutable?
Jeriho
  • 7,129
  • 9
  • 41
  • 57
83
votes
5 answers

Mutable variable is accessible from closure. How can I fix this?

I am using Typeahead by twitter. I am running into this warning from Intellij. This is causing the "window.location.href" for each link to be the last item in my list of items. How can I fix my code? Below is my code: AutoSuggest.prototype.config =…
iCodeLikeImDrunk
  • 17,085
  • 35
  • 108
  • 169
75
votes
10 answers

Are mutable hashmap keys a dangerous practice?

Is it bad practice to use mutable objects as Hashmap keys? What happens when you try to retrieve a value from a Hashmap using a key that has been modified enough to change its hashcode? For example, given class Key { int a; //mutable field …
donnyton
  • 5,874
  • 9
  • 42
  • 60
66
votes
5 answers

In Kotlin, how do you modify the contents of a list while iterating

I have a list: val someList = listOf(1, 20, 10, 55, 30, 22, 11, 0, 99) And I want to iterate it while modifying some of the values. I know I can do it with map but that makes a copy of the list. val copyOfList = someList.map { if (it <= 20) it +…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
61
votes
3 answers

Which JavaScript Array functions are mutating?

I am writing an Array-derived class in JavaScript and need to know which functions to overload so that I can be aware of changes made to the array. I know Array.push() and Array.splice() are mutating. Is there a definitive list of any others?
devios1
  • 36,899
  • 45
  • 162
  • 260
60
votes
1 answer

How to append or prepend on a Scala mutable.Seq

There's something I don't understand about Scala's collection.mutable.Seq. It describes the interface for all mutable sequences, yet I don't see methods to append or prepend elements without creating a new sequence. Am I missing something obvious…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
58
votes
10 answers

What is difference between mutable and immutable String in java

As per my knowledge, a mutable string can be changed, and an immutable string cannot be changed. Here I want to change the value of String like this, String str="Good"; str=str+" Morning"; and other way is, StringBuffer str= new…
Raghu
  • 1,324
  • 7
  • 24
  • 47
53
votes
8 answers

Mutable strings in Python

Do you know of a Python library which provides mutable strings? Google returned surprisingly few results. The only usable library I found is http://code.google.com/p/gapbuffer/ which is in C but I would prefer it to be written in pure Python. Edit:…
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117
52
votes
7 answers

Is the use of del bad?

I commonly use del in my code to delete objects: >>> array = [4, 6, 7, 'hello', 8] >>> del(array[array.index('hello')]) >>> array [4, 6, 7, 8] >>> But I have heard many people say that the use of del is unpythonic. Is using del bad practice? >>>…
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
49
votes
7 answers

Swift - How to mutate a struct object when iterating over it

I am still not sure about the rules of struct copy or reference. I want to mutate a struct object while iterating on it from an array: For instance in this case I would like to change the background color but the compiler is yelling at me struct…
Avba
  • 14,822
  • 20
  • 92
  • 192
47
votes
4 answers

Always declare std::mutex as mutable in C++11?

After watching Herb Sutter's talk You Don't Know const and mutable, I wonder whether I should always define a mutex as mutable? If yes, I guess the same holds for any synchronized container (e.g., tbb::concurrent_queue)? Some background: In his…
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
44
votes
2 answers

Why did Matz choose to make Strings mutable by default in Ruby?

It's the reverse of this question: Why can't strings be mutable in Java and .NET? Was this choice made in Ruby only because operations (appends and such) are efficient on mutable strings, or was there some other reason? (If it's only efficiency,…
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
1
2
3
78 79