mutable

Mutable vs Immutable for parallel applications [closed]

独自空忆成欢 提交于 2019-12-09 07:40:55
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . In the application I am writing, I need to write lots of base types, which will most likely be immutable. But I am wondering how mutable types compare in parallel applications to immutable ones. You can use locks with mutable objects, right? How does it compare to other

NSArray @property backed by a NSMutableArray

亡梦爱人 提交于 2019-12-09 07:35:05
问题 I've defined a class where I'd like a public property to appear as though it is backed by an NSArray . That is simple enough, but in my case the actual backing ivar is an NSMutableArray : @interface Foo { NSMutableArray* array; } @property (nonatomic, retain) NSArray* array; @end In my implementation file ( *.m ) I @synthesize the property but I immediately run into warnings because using self.words is the same as trying to modifying an NSArray . What is the correct way to do this? Thanks!

NSMutableDictionary: mutating method sent to immutable object

倖福魔咒の 提交于 2019-12-09 02:26:51
问题 The following code is returning an exception with the following error message "mutating method sent to immutable object" when attempting to removeObjectForKey NSMutableDictionary * storedIpDictionary = (NSMutableDictionary*)[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dictDeviceIp"]; NSString *key = self.currentDeviceNameText.text; NSString *ipAddressTemp = [storedIpDictionary objectForKey:key]; [storedIpDictionary removeObjectForKey:key]; <----Crashes here storedIpDictionary[key

Mutable HashMap with a mutable default value doesn't keep the changes [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-08 13:07:21
问题 This question already has an answer here : Update mutable HashMap value which is a mutable collection (1 answer) Closed last year . Suppose that I want a mutable HashMap[Int, HashSet[Int]] that has integers as keys mutable hash sets of integers as values I want that an empty mutable HashSet is created by default whenever a value for a new key is accessed or updated. Here is what I tried: import collection.mutable.{HashMap, HashSet} val hm = HashMap .empty[Int, HashSet[Int]] .withDefault(_ =>

How do I initialize a struct field which is a mutable reference to an Option?

ε祈祈猫儿з 提交于 2019-12-08 06:42:27
问题 How do I initialize a struct field which is a mutable reference to an Option<T> ? Here is my struct: pub struct Cmd<'a> { pub exec: String, pub args: &'a mut Option<Vec<String>>, } I tried to initialize this struct like this: let cmd = Cmd { exec: String::from("whoami"), args: None, }; But I get the following error: error[E0308]: mismatched types --> src/main.rs:9:15 | 9 | args: None, | ^^^^ expected mutable reference, found enum `std::option::Option` | = note: expected type `&mut std::option

Optimizing mutable array state heavy manipulation code

末鹿安然 提交于 2019-12-06 10:49:56
I've been trying to complete this exercise on hackerrank in time. But my following Haskell solution fails on test case 13 to 15 due to time out. My Haskell solution import Data.Vector(Vector(..),fromList,(!),(//),toList) import Data.Vector.Mutable import qualified Data.Vector as V import Data.ByteString.Lazy.Char8 (ByteString(..)) import qualified Data.ByteString.Lazy.Char8 as L import Data.ByteString.Lazy.Builder import Data.Maybe import Control.Applicative import Data.Monoid import Prelude hiding (length) readInt' = fst . fromJust . L.readInt toB [] = mempty toB (x:xs) = string8 (show x) <>

Mutable Objects in Collections

寵の児 提交于 2019-12-06 05:16:04
From the docs , Storing mutable objects in collection objects can cause problems. Certain collections can become invalid or even corrupt if objects they contain mutate because, by mutating, these objects can affect the way they are placed in the collection. First, the properties of objects that are keys in hashing collections such as NSDictionary objects or NSSet objects will, if changed, corrupt the collection if the changed properties affect the results of the object’s hash or isEqual: methods. (If the hash method of the objects in the collection does not depend on their internal state,

How do you return multiple values and assign them to mutable variables?

橙三吉。 提交于 2019-12-05 16:04:13
问题 This is what I have so far. let Swap (left : int , right : int ) = (right, left) let mutable x = 5 let mutable y = 10 let (newX, newY) = Swap(x, y) //<--this works //none of these seem to work //x, y <- Swap(x, y) //(x, y) <- Swap(x, y) //(x, y) <- Swap(x, y) //do (x, y) = Swap(x, y) //let (x, y) = Swap(x, y) //do (x, y) <- Swap(x, y) //let (x, y) <- Swap(x, y) 回答1: You can't; there's no syntax to update 'more than one mutable variable' with a single assignment. Of course you can do let newX,

How do you declare the values of a dictionary entry as mutable?

对着背影说爱祢 提交于 2019-12-05 14:11:37
问题 The Google yields plenty of example of adding and deleting entries in an F# dictionary (or other collection). But I don't see examples to the equivalent of myDict["Key"] = MyValue; I've tried myDict.["Key"] <- MyValue I have also attempted to declare the Dictionary as Dictionary<string, mutable string> as well several variants on this. However, I haven't hit on the correct combination yet... if it is actually possible in F#. Edit: The offending code is: type Config(?fileName : string) = let

lazy list computed using mutable state?

孤街浪徒 提交于 2019-12-05 10:07:41
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 arr i when ( ai ) $ forM_ [ i^2 , i^2 + i .. n ] $ \j -> do writeArray arr j False -- yield i ???