mutable

Mutable values in an object

ぐ巨炮叔叔 提交于 2019-12-10 18:16:13
问题 In Scala, if I'm planning on having a mutable attribute (e.g. a bag of numbers) of an object, when is it appropriate to Create a var and use an immutable data structure? Create a val and use a mutable data structure? I'm going to throw a guess out that you'd want to use #2 for threaded applications? Are some of the collections thread-safe? How about in general? (Or does it not really matter?) 回答1: Between your choices 1 and 2, it doesn't matter - mutable is mutable, and if you read or modify

Mutating Operator Error After Changing to Swift 3, Issue Researched, but can't solve

若如初见. 提交于 2019-12-10 16:06:01
问题 I am getting a "left side of mutating operator isn't mutable "..<" returns immutable value" error I read the other post regarding mutating values but I can't figure out how those solutions apply. Code (and comments): //populate array of 3 random numbers using correct answer and 2 incorrect choices func insertIntoArray3(_ randomNumber: Int) -> Int { for intJ in 0 ..< 2 += 1{ if arrayIndex != 3 { checkIfExists(randomNumber) if ifExists { let randomNumber = 1 + random() % 10 insertIntoArray3

Dealing with lazy computation in C++ classes

为君一笑 提交于 2019-12-10 15:18:10
问题 Let's say I have a class: class NumberCollection { public: typedef std::set<int> SetType; typedef SetType::iterator iterator; void insert(int n); iterator begin(); iterator end(); size_t size() const; iterator difficultBegin(); iterator difficultEnd(); size_t difficultSize() const; private: SetType easySet_, difficultSet_; } Where insert() adds an element to easySet_ . difficultSet_ 's members change depending on the members of easySet_ . The problem I am having is that, multiple insertions

Is there a writable iterator in Java?

烈酒焚心 提交于 2019-12-10 13:54:03
问题 In C+ one can use iterators for writing to a sequence. Simplest example would be: vector<int> v; for (vector<int>::iterator it = v.begin(); it!=v.end(); ++it) { *it = 42; } I need something more complicated - keep iterator as a class member for a later use. But I don't know how to get this behavior from Java iterators. Are there writable iterators in Java at all? If not then what replaces them? 回答1: The ListIterator (which you can obtain by List#listIterator()) has add() and set() methods

No Scala mutable list

强颜欢笑 提交于 2019-12-10 12:37:44
问题 Scala has both a mutable and an immutable Map , but it has only an immutable List. If you want a mutable List you need a ListBuffer. I don't understand why this is so. Any one knows?. 回答1: You can choose between these: scala.collection.mutable.DoubleLinkedList scala.collection.mutable.LinkedList scala.collection.mutable.ListBuffer scala.collection.mutable.MutableList So, yes, Scala has mutable lists :-) 回答2: I hope that this article may be of some use to you. The diagram at the bottom of the

lazy list computed using mutable state?

断了今生、忘了曾经 提交于 2019-12-10 05:24:34
问题 I'd like to figure out in general how to use mutable state in the computation of lazy lists. For instance, here is a naive Sieve of Eratosthenes implemented using a mutable array (source): import Control.Monad.ST import Data.Array.ST import Data.Array.Unboxed import Control.Monad import Data.List prime :: Int -> UArray Int Bool prime n = runSTUArray $ do arr <- newArray ( 2 , n ) True :: ST s ( STUArray s Int Bool ) forM_ ( takeWhile ( \x -> x*x <= n ) [ 2 .. n ] ) $ \i -> do ai <- readArray

Are String Arrays mutable?

人走茶凉 提交于 2019-12-10 03:59:43
问题 I wonder if String arrays in Java are mutable ? I know that Strings are immutable, but how about string Arrays ? If I have a string array, and change the content, will a new string object be created ? Or will the actual value just be changed ? Thanks in advance 回答1: The String s contained in the String[] are indeed immutable, but the array is mutable. This is well explained in this answer: Immutability means that objects of a certain type can not change in any meaningful way to outside

Are new vectors created even if the old ones aren't used anymore?

心不动则不痛 提交于 2019-12-10 03:56:35
问题 This question is about the Data.Vector package. Given the fact that I'll never use the old value of a certain cell once the cell is updated. Will the update operation always create a new vector, reflecting the update, or will it be done as an in-place update ? Note: I know about Data.Vector.Mutable 回答1: No, but something even better can happen. Data.Vector is built using "stream fusion". This means that if the sequence of operations that you are performing to build up and then tear down the

Understanding immutable composite types with fields of mutable types in Julia

こ雲淡風輕ζ 提交于 2019-12-10 03:24:23
问题 Initial note: I'm working in Julia, but this question probably applies to many languages. Setup: I have a composite type as follows: type MyType x::Vector{String} end I write some methods to act on MyType . For example, I write a method that allows me to insert a new element in x , e.g. function insert!(d::MyType, itemToInsert::String) . Question: Should MyType be mutable or immutable? My understanding: I've read the Julia docs on this, as well as more general (and highly upvoted) questions

Hashtable of mutable variable in Ocaml

耗尽温柔 提交于 2019-12-09 18:14:53
问题 I need to use hashtable of mutable variable in Ocaml, but it doesn't work out. let link = Hashtbl.create 3;; let a = ref [1;2];; let b = ref [3;4];; Hashtbl.add link a b;; # Hashtbl.mem link a;; - : bool = true # a := 5::!a;; - : unit = () # Hashtbl.mem link a;; - : bool = false Is there any way to make it works? 回答1: Mutable variables that may happen to have the same content can still be distinguished because they are stored at different locations in memory. They can be compared with the