immutability

Immutable/Mutable Collections in Swift

自作多情 提交于 2019-11-26 11:58:10
问题 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 see the equivalents in Swift for the following in Objective-C Immutable Array NSArray *imArray = [[NSArray alloc]initWithObjects:@\"First\",@\"Second\",@\"Third\",nil]; Mutable Array NSMutableArray *mArray = [[NSMutableArray alloc]initWithObjects:@

Why do we need immutable class?

跟風遠走 提交于 2019-11-26 11:37:58
I am unable to get what are the scenarios where we need an immutable class. Have you ever faced any such requirement? or can you please give us any real example where we should use this pattern. The other answers seem to focused on explaining why immutability is good. It is very good and I use it whenever possible. However, that is not your question . I'll take your question point by point to try to make sure you're getting the answers and examples you need. I am unable to get what are the scenarios where we need an immutable class. "Need" is a relative term here. Immutable classes are a

How to create immutable objects in Java?

扶醉桌前 提交于 2019-11-26 11:35:38
How to create immutable objects in Java? Which objects should be called immutable? If I have class with all static members is it immutable? Below are the hard requirements of an immutable object. Make the class final make all members final, set them explicitly, in a static block, or in the constructor Make all members private No Methods that modify state Be extremely careful to limit access to mutable members(remember the field may be final but the object can still be mutable. ie private final Date imStillMutable ). You should make defensive copies in these cases. The reasoning behind making

What would a “frozen dict” be?

大憨熊 提交于 2019-11-26 11:11:58
A frozen set is a frozenset. A frozen list could be a tuple. What would a frozen dict be? An immutable, hashable dict. I guess it could be something like collections.namedtuple , but that is more like a frozen-keys dict (a half-frozen dict). Isn't it? A "frozendict" should be a frozen dictionary, it should have keys , values , get , etc., and support in , for , etc. Python doesn't have a builtin frozendict type. It turns out this wouldn't be useful too often (though it would still probably be useful more often than frozenset is). The most common reason to want such a type is when memoizing

Efficient implementation of immutable (double) LinkedList

强颜欢笑 提交于 2019-11-26 11:07:49
问题 Having read this question Immutable or not immutable? and reading answers to my previous questions on immutability, I am still a bit puzzled about efficient implementation of simple LinkedList that is immutable. In terms of array tha seems to be easy - copy the array and return new structure based on that copy. Supposedly we have a general class of Node: class Node{ private Object value; private Node next; } And class LinkedList based on the above allowing the user to add, remove etc. Now,

What's the best name for a non-mutating “add” method on an immutable collection?

五迷三道 提交于 2019-11-26 10:09:40
问题 Sorry for the waffly title - if I could come up with a concise title, I wouldn\'t have to ask the question. Suppose I have an immutable list type. It has an operation Foo(x) which returns a new immutable list with the specified argument as an extra element at the end. So to build up a list of strings with values \"Hello\", \"immutable\", \"world\" you could write: var empty = new ImmutableList<string>(); var list1 = empty.Foo(\"Hello\"); var list2 = list1.Foo(\"immutable\"); var list3 = list2

What does immutable mean?

浪子不回头ぞ 提交于 2019-11-26 10:07:48
问题 If a string is immutable, does that mean that.... (let\'s assume JavaScript) var str = \'foo\'; alert(str.substr(1)); // oo alert(str); // foo Does it mean, when calling methods on a string, it will return the modified string, but it won\'t change the initial string? If the string was mutable, does that mean the 2nd alert() would return oo as well? 回答1: It means that once you instantiate the object, you can't change its properties. In your first alert you aren't changing foo. You're creating

Python references

一笑奈何 提交于 2019-11-26 09:53:38
问题 Can someone explain why the example with integers results in different values for x and y and the example with the list results in x and y being the same object? x = 42 y = x x = x + 1 print x # 43 print y # 42 x = [ 1, 2, 3 ] y = x x[0] = 4 print x # [4, 2, 3] print y # [4, 2, 3] x is y # True 回答1: Because integers are immutable, while list are mutable. You can see from the syntax. In x = x + 1 you are actually assigning a new value to x (it is alone on the LHS). In x[0] = 4 , you're calling

Why can tuples contain mutable items?

情到浓时终转凉″ 提交于 2019-11-26 09:51:39
If a tuple is immutable then why can it contain mutable items? It is seemingly a contradiction that when a mutable item such as a list does get modified, the tuple it belongs to maintains being immutable. Raymond Hettinger That's an excellent question. The key insight is that tuples have no way of knowing whether the objects inside them are mutable. The only thing that makes an object mutable is to have a method that alters its data. In general, there is no way to detect this. Another insight is that Python's containers don't actually contain anything. Instead, they keep references to other

Why is immutability so important (or needed) in JavaScript?

南笙酒味 提交于 2019-11-26 08:54:26
问题 I am currently working on React JS and React Native frameworks. On the half way road I came across Immutability or the Immutable-JS library, when I was reading about Facebook\'s Flux and Redux implementation. The question is, why is immutability so important? What is wrong in mutating objects? Doesn\'t it make things simple? Giving an example, let us consider a simple News reader app with the opening screen being a list view of news headlines. If I set say an array of objects with a value