immutability

Define a python dictionary with immutable keys but mutable values

寵の児 提交于 2019-11-30 18:32:27
Well, the question is in the title: how do I define a python dictionary with immutable keys but mutable values? I came up with this (in python 2.x): class FixedDict(dict): """ A dictionary with a fixed set of keys """ def __init__(self, dictionary): dict.__init__(self) for key in dictionary.keys(): dict.__setitem__(self, key, dictionary[key]) def __setitem__(self, key, item): if key not in self: raise KeyError("The key '" +key+"' is not defined") dict.__setitem__(self, key, item) but it looks to me (unsurprisingly) rather sloppy. In particular, is this safe or is there the risk of actually

pre-release Collections.Immutable

那年仲夏 提交于 2019-11-30 17:59:25
Has anyone succeeded in opening the pre-release System.Collections.Immutable from NuGet in F#? I'm getting this error: The type 'IEnumerable`1' is required here and is unavailable. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Even though my project is .NET 4.5 Tried explicitly opening System.Runtime and that did not work either. I am referencing .NETCore\v4.5\System.Runtime.dll This is a known issue that the relevant teams at Microsoft are investigating. In the meantime, here are the workaround steps: Create an F#

java: advantages of immutable objects in examples [closed]

五迷三道 提交于 2019-11-30 17:50:11
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . give me please examples where I could see advantages of immutable objects. Info I found in internet are concentrated in threads. I don

Why exactly does akka need immutable messages

前提是你 提交于 2019-11-30 17:41:35
问题 or put another way: are there proper uses of mutable messages? The use case I am facing is that I want to process objects which basically are of type Map<String,List<String>> The typical processing an actor would do is to read or write some of the fields of the map. A condensed example is (leaving out null -tests etc.) map.get("somekey").add("new value"); My hunch is that keeping this immutable in Scala would be trivial by using the respective collection types. In Java it would require to go

How/why does set() in {frozenset()} work?

有些话、适合烂在心里 提交于 2019-11-30 17:40:42
Even though sets are unhashable, membership check in other set works: >>> set() in {frozenset()} True I expected TypeError: unhashable type: 'set' , consistent with other behaviours in Python: >>> set() in {} # doesn't work when checking in dict TypeError: unhashable type: 'set' >>> {} in {frozenset()} # looking up some other unhashable type doesn't work TypeError: unhashable type: 'dict' So, how is set membership in other set implemented? The last line of the documentation for set s discusses this: Note, the elem argument to the __contains__() , remove() , and discard() methods may be a set .

Is there an easy way to make an immutable version of a class?

泪湿孤枕 提交于 2019-11-30 16:51:42
问题 Is there an easy way to make an instance immutable? Let's do an example, I have a class holding a lots of data fields (only data, no behavior): class MyObject { // lots of fields painful to initialize all at once // so we make fields mutable : public String Title { get; set; } public String Author { get; set; } // ... } Example of creation: MyObject CreationExample(String someParameters) { var obj = new MyObject { Title = "foo" // lots of fields initialization }; // even more fields

Undo/Redo with immutable objects

核能气质少年 提交于 2019-11-30 14:33:05
I read the following in an article Immutable objects are particularly handy for implementing certain common idioms such as undo/redo and abortable transactions. Take undo for example. A common technique for implementing undo is to keep a stack of objects that somehow know how to run each command in reverse (the so-called "Command Pattern"). However, figuring out how to run a command in reverse can be tricky. A simpler technique is to maintain a stack of immutable objects representing the state of the system between successive commands. Then, to undo a command, you simply revert back to the

Is Initialization On Demand Holder idiom thread safe without a final modifier

给你一囗甜甜゛ 提交于 2019-11-30 14:03:09
问题 I have a hunch that using the holder idiom without declaring the holder field as final is not thread safe (due to the way immutability works in Java). Can somebody confirm this (hopefully with some sources)? public class Something { private long answer = 1; private Something() { answer += 10; answer += 10; } public int getAnswer() { return answer; } private static class LazyHolder { // notice no final private static Something INSTANCE = new Something(); } public static Something getInstance()

Any weak interning collections (for immutable objects)

别说谁变了你拦得住时间么 提交于 2019-11-30 13:34:11
问题 In some situations involving immutable objects, it will be possible for many distinct objects to come into existence which are semantically identical. A simple example would be reading many lines of text from a file into strings. From the program's perspective, the fact that two lines have the same sequence of characters would be "coincidence", but from the programmer's perspective a large amount of duplication may be expected. If many string instances are identical, changing the references

Why are immutable objecs loved by JVM's GC?

丶灬走出姿态 提交于 2019-11-30 13:10:43
I know the reason that JVM GC loves short-live object because it can be collected in minor GC. But why does JVM GC love immutable objects? EDIT: Charlie Hunt says that GC loves immutable objects in his presentation . Thanks If the GC can know that an object doesn't contain any references to any gen0 objects, then it can be ignored when performing a gen0 collection. Likewise if an object doesn't contain any reference to any gen0 or gen1 objects, it may be ignored when performing a gen1 collection. The more objects can be ignored during a collection, the faster that collection will be. If an