immutability

Why might a System.String object not cache its hash code?

余生长醉 提交于 2019-11-28 22:25:09
A glance at the source code for string.GetHashCode using Reflector reveals the following (for mscorlib.dll version 4.0): public override unsafe int GetHashCode() { fixed (char* str = ((char*) this)) { char* chPtr = str; int num = 0x15051505; int num2 = num; int* numPtr = (int*) chPtr; for (int i = this.Length; i > 0; i -= 4) { num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0]; if (i <= 2) { break; } num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1]; numPtr += 2; } return (num + (num2 * 0x5d588b65)); } } Now, I realize that the implementation of GetHashCode is not specified and is

Is there a way to Object.freeze() a JavaScript Date?

匆匆过客 提交于 2019-11-28 22:12:18
According to MDN Object.freeze() documentation : The Object.freeze() method freezes an object: that is, prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed. In essence the object is made effectively immutable. The method returns the object being frozen. I was expecting that calling freeze on a date would prevent changes to that date, but it does not appear to be working. Here's what I am doing (running Node.js v5.3.0): let d = new Date()

persistent vs immutable data structure

馋奶兔 提交于 2019-11-28 21:17:50
问题 Is there any difference in a persistent and immutable data structure? Wikipedia refers to immutable data structure when discussing persistence but I have a feeling there might be a subtle difference between the two. 回答1: Immutability is an implementation technique. Among other things, it provides persistence , which is an interface. The persistence API is something like: version update(operation o, version v) performs operation o on version v , returning a new version. If the data structure

Remove value from object without mutation

…衆ロ難τιáo~ 提交于 2019-11-28 20:43:16
问题 What's a good and short way to remove a value from an object at a specific key without mutating the original object? I'd like to do something like: let o = {firstname: 'Jane', lastname: 'Doe'}; let o2 = doSomething(o, 'lastname'); console.log(o.lastname); // 'Doe' console.log(o2.lastname); // undefined I know there are a lot of immutability libraries for such tasks, but I'd like to get away without a library. But to do this, a requirement would be to have an easy and short way that can be

Immutability and XML Serialization

Deadly 提交于 2019-11-28 19:44:04
问题 I have several classes that are immutable once their initial values are set. Eric Lippert calls this write-once immutability. Implementing write-once immutability in C# usually means setting the initial values via the constructor. These values initialize readonly fields. But if you need to serialize a class like this to XML, using either the XmlSerializer or the DataContractSerializer, you must have a parameterless constructor. Does anyone have suggestions for how to work around this problem?

Why should objects in Redux be immutable?

一笑奈何 提交于 2019-11-28 19:10:31
Why should objects in Redux be immutable? I know that some frameworks such as Angular2 will use onPush and can take advantage of immutability to compare states of views for faster rendering, but I am wondering if there are other reasons as Redux is framework agnostic and yet it mentions within its own docs to use immutability (regardless of the framework). Appreciate any feedback. Ashley Coolman Redux is a small library that represents state as (immutable) objects. And new states by passing the current state through pure functions to create an entirely new object/application states. If your

How to create immutable objects in C#?

人盡茶涼 提交于 2019-11-28 18:53:58
问题 In a question about Best practices for C# pattern validation, the highest voted answer says: I tend to perform all of my validation in the constructor. This is a must because I almost always create immutable objects. How exactly do you create an immutable object in C#? Do you just use the readonly keyword? How exactly would this work if you want to validate in the constructor of your Entity Framework generated model class? Would it look like below? public partial readonly Person { public

What's the difference between Collections.unmodifiableSet() and ImmutableSet of Guava?

孤街浪徒 提交于 2019-11-28 18:10:54
JavaDoc of ImmutableSet says: Unlike Collections.unmodifiableSet , which is a view of a separate collection that can still change, an instance of this class contains its own private data and will never change. This class is convenient for public static final sets ("constant sets") and also lets you easily make a "defensive copy" of a set provided to your class by a caller. But the ImmutableSet still stores reference of elements, I couldn't figure out the difference to Collections.unmodifiableSet() . Sample: StringBuffer s=new StringBuffer("a"); ImmutableSet<StringBuffer> set= ImmutableSet.of(s

Idiomatic way to update value in a Map based on previous value

拟墨画扇 提交于 2019-11-28 17:46:44
Let's say I store bank accounts information in an immutable Map : val m = Map("Mark" -> 100, "Jonathan" -> 350, "Bob" -> 65) and I want to withdraw, say, $50 from Mark's account. I can do it as follows: val m2 = m + ("Mark" -> (m("Mark") - 50)) But this code seems ugly to me. Is there better way to write this? There's no adjust in the Map API, unfortunately. I've sometimes used a function like the following (modeled on Haskell's Data.Map.adjust , with a different order of arguments): def adjust[A, B](m: Map[A, B], k: A)(f: B => B) = m.updated(k, f(m(k))) Now adjust(m, "Mark")(_ - 50) does what

Functional programming: state vs. reassignment

三世轮回 提交于 2019-11-28 17:38:55
I need help getting my head around the difference between my current OOP notion of state, and the way it would be done in a functional language like Haskell or Clojure. To use a hackneyed example, let's say we're dealing with simplified bank account objects/structs/whatever. In an OOP language, I'd have some class holding a reference to a BankAccount, which would have instance variables for things like interest rate, and methods like setInterestRate() which change the object's state and generally return nothing. In say Clojure, I'd have a bank-account struct (a glorified hashmap), and special