mutable

Basic F# questions: mutability, capitalization standards, functions vs. methods

Feel free to point me to other answers if these have already been asked! I'm just starting F# with the new release this month. I've got some background in both OO and functional languages (Haskell and Scheme, but not OCaml/ML). A couple of questions have arisen so far from reading through the little tutorial thing that comes with the F#...

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

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...

C# design for an object where some properties are expensive: excuse to make it mutable?

Yes, I know, yet another question about mutable objects. See this for general background and this for the closest analogue to my question. (though it has some C++ specific overtones that don't apply here) Let's assume that the following pseudo code represents the best interface design. That is, it's the clearest expression of the busin...

Clojure mutable storage types

I'm attempting to learn Clojure from the API and documentation available on the site. I'm a bit unclear about mutable storage in Clojure and I want to make sure my understanding is correct. Please let me know if there are any ideas that I've gotten wrong. Edit: I'm updating this as I receive comments on its correctness. Disclaimer: A...

mutable fields for objects in a Java Set

Am I correct in assuming that if you have an object that is contained inside a Java Set<> (or as a key in a Map<> for that matter), any fields that are used to determine identity or relation (via hashCode(), equals(), compareTo() etc.) cannot be changed without causing unspecified behavior for operations on the collection? (edit: as allu...

pitfalls of collections of entities in Hibernate

OK, this is a follow-up question to this one, since I am really confused now. Suppose I have a one-to-many or many-to-many association between entities Person and Event such that a Person class in Java contains a Set<Event>. (Let's ignore whether Event contains a single Person or a Set<Person>.) Events are entities stored in a database...

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

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 o...

Mutating a lock object

Hi Guys Just curious to know (in as much detail as possible), why is it a bad practice to modify the object while using it as a lock. //Assuming the lockObject is globally available synchronized(lockObject){ lockObject.someMutativeOperation(...); } Cheers ...

Python object intialization bug. Or am I misunderstanding how objects work?

1 import sys 2 3 class dummy(object): 4 def __init__(self, val): 5 self.val = val 6 7 class myobj(object): 8 def __init__(self, resources): 9 self._resources = resources 10 11 class ext(myobj): 12 def __init__(self, resources=[]): 13 #myobj.__init__(self, resources) 14 ...

How to restrict access to mutable or immutable methods?

Hi In a new Java project I try to apply as much best practices as possible. The one I'm having problems with is immutability. Although I understood the concept and already built some immutable classes I now came to a class where I think it's more appropriate to do it as a mutable class. The main problem is that I want to hide the mutab...

Mutable Data in OCaml

I've created a mutable data structure in OCaml, however when I go to access it, it gives a weird error, Here is my code type vector = {a:float;b:float};; type vec_store = {mutable seq:vector array;mutable size:int};; let max_seq_length = ref 200;; exception Out_of_bounds;; exception Vec_store_full;; let vec_mag {a=c;b=d} = sqrt( c*...

Any way to make a mutable object derived from an immutable object in C#?

I'm interested in making an immutable class that has properties that cannot be modified, and a mutable class that derives from it. These objects would be simple data objects representing database records, so they would have only immutable values for the properties. My goal is to have my data access layer create the mutable versions of ...

Using NSArray with Monotouch

Hi, How to insert items into NSArray object in C# (Monotouch)? I don't find appropriate method to do so? In Objective-C side, there is a constructor called "initWithObjects" but I don't find this on C# side. pom ...

How would you convert this mutable tree into an immutable one?

How would you convert type Node into an immutable tree? This class implements a range tree that does not allow overlapping or adjacent ranges and instead joins them. For example if the root node is {min = 10; max = 20} then it's right child and all its grandchildren must have a min and max value greater than 21. The max value of a rang...

Object appended to a list instance appears in a different instance of that list.

Hi, I was writing this little piece of code as an exercise in object-oriented programming. Here I'm trying to define a house as a list of rooms and each room as a list of devices (lamps, for example). First I created all the objects and them appended the two rooms to the house and a different device to each room. Pretty basic. The pro...

Problem creating N*N*N list in Python

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, ...

An efficient technique to replace an occurence in a sequence with mutable or immutable state

I am searching for an efficient a technique to find a sequence of Op occurences in a Seq[Op]. Once an occurence is found, I want to replace the occurence with a defined replacement and run the same search again until the list stops changing. Scenario: I have three types of Op case classes. Pop() extends Op, Push() extends Op and Nop()...

Cocoa: Testing to find if an NSString is immutable or mutable?

This produces an immutable string object: NSString* myStringA = @"A"; //CORRECTED FROM: NSMutableString* myStringA = @"A"; This produces a mutable string object: NSMutableString* myStringB = [NSMutableString stringWithString:@"B"]; But both objects are reported as the same kind of object, "NSCFString": NSLog(@"myStringA is type: ...

how to read immutable data structures from file in scala

I have a data structure made of Jobs each containing a set of Tasks. Both Job and Task data are defined in files like these: jobs.txt: JA JB JC tasks.txt: JB T2 JA T1 JC T1 JA T3 JA T2 JB T1 The process of creating objects is the following: - read each job, create it and store it by id - read task, retrieve job by id, create t...

Haskell mutable map/tree

I am looking for a mutable (balanced) tree/map/hash table in Haskell or a way how to simulate it inside a function. I.e. when I call the same function several times, the structure is preserved. So far I have tried Data.HashTable (which is OK, but somewhat slow) and tried Data.Array.Judy but I was unable to make it work with GHC 6.10.4. A...