immutability

effect of changing String using reflection

徘徊边缘 提交于 2019-11-26 21:33:42
问题 As we all know, String is immutable in java. however, one can change it using reflection, by getting the Field and setting access level. (I know it is unadvised, I am not planning to do so, this question is pure theoretical). my question: assuming I know what I am doing (and modify all fields as needed), will the program run properly? or does the jvm makes some optimizations that rely on String being immutable? will I suffer performance loss? if so, what assumption does it make? what will go

val-mutable versus var-immutable in Scala

时光毁灭记忆、已成空白 提交于 2019-11-26 21:28:59
Are there any guidelines in Scala on when to use val with a mutable collection versus using var with an immutable collection? Or should you really aim for val with an immutable collection? The fact that there are both types of collection gives me a lot of choice, and often I don't know how to make that choice. Pretty common question, this one. The hard thing is finding the duplicates. You should strive for referential transparency . What that means is that, if I have an expression "e", I could make a val x = e , and replace e with x . This is the property that mutability break. Whenever you

How do I create an immutable Class?

会有一股神秘感。 提交于 2019-11-26 21:22:09
I am working on creating an immutable class. I have marked all the properties as read-only. I have a list of items in the class. Although if the property is read-only the list can be modified. Exposing the IEnumerable of the list makes it immutable. I wanted to know what is the basic rules one has to follow to make a class immutable ? I think you're on the right track - all information injected into the class should be supplied in the constructor all properties should be getters only if a collection (or Array) is passed into the constructor, it should be copied to keep the caller from

Efficient, Immutable, Extensible Collections for .NET [duplicate]

雨燕双飞 提交于 2019-11-26 21:19:02
问题 This question already has an answer here: Immutable collections? 10 answers It seems to me there is an extreme lack of safe, immutable collection types for .NET, in particular BCL but I've not seen much work done outside either. Do anyone have any pointers to a (preferably) production quality, fast, immutable collections library for .NET. A fast list type is essential. I'm not yet prepared to switch to F#. *Edit: Note to searchers, this is being rolled into the BCL soon: .NET immutable

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

冷暖自知 提交于 2019-11-26 21:13:44
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 initially I can't manipulate it. That's what immutability principle says, right? (Correct me if I am wrong.

Why are Integers immutable in Java?

非 Y 不嫁゛ 提交于 2019-11-26 20:56:57
问题 I know Integers are immutable in Java. But why it is designed this way? I went through other answers before asking this question: Is Integer Immutable i++ still working for immutable Integer in Java? Why are Java wrapper classes immutable? But I couldn't find the use case which mandates the Integer to be immutable. Are there any technical reasons like there are for String? String is used as parameter in network connection, database URLs etc. It could easily be compromised if it was mutable.

Pandas: Modify a particular level of Multiindex

你说的曾经没有我的故事 提交于 2019-11-26 19:58:21
问题 I have a dataframe with Multiindex and would like to modify one particular level of the Multiindex. For instance, the first level might be strings and I may want to remove the white spaces from that index level: df.index.levels[1] = [x.replace(' ', '') for x in df.index.levels[1]] However, the code above results in an error: TypeError: 'FrozenList' does not support mutable operations. I know I can reset_index and modify the column and then re-create the Multiindex, but I wonder whether there

Why are immutable objects thread-safe?

こ雲淡風輕ζ 提交于 2019-11-26 19:49:39
问题 class Unit { private readonly string name; private readonly double scale; public Unit(string name, double scale) { this.name = name; this.scale = scale, } public string Name { get { return name; } } public string Scale { get { return scale; } } private static Unit gram = new Unit("Gram", 1.0); public Unit Gram { get { return gram; } } } Multiple threads have access to Unit.Gram . Why is it ok for multiple threads simultaneously read Unit.Gram.Title ? My concern is that they are referring to

Should IEquatable<T>, IComparable<T> be implemented on non-sealed classes?

不想你离开。 提交于 2019-11-26 19:18:25
问题 Anyone have any opinions on whether or not IEquatable<T> or IComparable<T> should generally require that T is sealed (if it's a class )? This question occurred to me since I'm writing a set of base classes intended to aid in the implementation of immutable classes. Part of the functionality which the base class is intended to provide is automatic implementation of equality comparisons (using the class's fields together with attributes which can be applied to fields to control equality

Advantages of stateless programming?

安稳与你 提交于 2019-11-26 19:15:14
I've recently been learning about functional programming (specifically Haskell, but I've gone through tutorials on Lisp and Erlang as well). While I found the concepts very enlightening, I still don't see the practical side of the "no side effects" concept. What are the practical advantages of it? I'm trying to think in the functional mindset, but there are some situations that just seem overly complex without the ability to save state in an easy way (I don't consider Haskell's monads 'easy'). Is it worth continuing to learn Haskell (or another purely functional language) in-depth? Is