immutability

How to avoid “too many parameters” problem in API design?

我怕爱的太早我们不能终老 提交于 2019-11-27 02:23:06
I have this API function: public ResultEnum DoSomeAction(string a, string b, DateTime c, OtherEnum d, string e, string f, out Guid code) I don't like it. Because parameter order becomes unnecessarily significant. It becomes harder to add new fields. It's harder to see what's being passed around. It's harder to refactor method into smaller parts because it creates another overhead of passing all the parameters in sub functions. Code is harder to read. I came up with the most obvious idea: have an object encapsulating the data and pass it around instead of passing each parameter one by one. Here

Python references

↘锁芯ラ 提交于 2019-11-27 02:04:23
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 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 the index operator on the list and giving it a parameter - it's actually equivalent to x.__setitem__(0, 4)

Immutable or not immutable?

巧了我就是萌 提交于 2019-11-27 01:37:44
问题 Ok, as I understand it, immutable types are inherently thread safe or so I've read in various places and I think I understand why it is so. If the inner state of an instance can not be modified once the object is created there seems to be no problems with concurrent access to the instance itself. Therefore, I could create the following List : class ImmutableList<T>: IEnumerable<T> { readonly List<T> innerList; public ImmutableList(IEnumerable<T> collection) { this.innerList = new List<T>

What are disadvantages to using immutable state in React?

蹲街弑〆低调 提交于 2019-11-27 01:32:38
问题 I have built my first React application with stateful stores the "normal" way, and now I am looking into using an immutable global state like used in the Este starterkit. The state of all stores is kept together in a single immutable data structure Components have no state but access data in their render() based on a store getter function Stores are also stateless but mutate the global application state for their domain using a cursor. The top level app component listens for state changes,

safe publication and the advantage of being immutable vs. effectively immutable

杀马特。学长 韩版系。学妹 提交于 2019-11-27 01:31:52
问题 I'm re-reading Java Concurrency In Practice, and I'm not sure I fully understand the chapter about immutability and safe publication. What the book says is: Immutable objects can be used safely by any thread without additional synchronization, even when synchronization is not used to publish them. What I don't understand is, why would anyone ( interested in making his code correct ) publish some reference unsafely? If the object is immutable, and it's published unsafely, I understand that any

Why should objects in Redux be immutable?

好久不见. 提交于 2019-11-27 01:15:21
问题 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. 回答1: Redux is a small library that represents state as (immutable) objects. And new states by passing the current

Are strings in Ruby mutable? [duplicate]

只谈情不闲聊 提交于 2019-11-27 01:15:13
问题 This question already has an answer here: Are strings mutable in Ruby? 2 answers Consider the following code: $ irb > s = "asd" > s.object_id # prints 2171223360 > s[0] = ?z # s is now "zsd" > s.object_id # prints 2171223360 (same as before) > s += "hello" # s is now "zsdhello" > s.object_id # prints 2171224560 (now it's different) Seems like individual characters can be changed w/o creating a new string. However appending to the string apparently creates a new string. Are strings in Ruby

What is the “pin” operator for, and are Elixir variables mutable?

对着背影说爱祢 提交于 2019-11-27 00:52:03
问题 Currently trying to understand the "^" operator in Elixir. From the website: The pin operator ^ can be used when there is no interest in rebinding a variable but rather in matching against its value prior to the match: Source - http://elixir-lang.org/getting_started/4.html With this in mind, you can attach a new value to a symbol like so: iex> x = 1 # Outputs "1" iex> x = 2 # Outputs "2" I can also do: iex> x = x + 1 # Outputs "3"! So my first question is; Are Elixir variables mutable? It

Why are immutable objects in hashmaps so effective?

♀尐吖头ヾ 提交于 2019-11-27 00:42:40
问题 So I read about HashMap. At one point it was noted: "Immutability also allows caching the hashcode of different keys which makes the overall retrieval process very fast and suggest that String and various wrapper classes (e.g., Integer ) provided by Java Collection API are very good HashMap keys." I don't quite understand... why? 回答1: String#hashCode : private int hash; ... public int hashCode() { int h = hash; if (h == 0 && count > 0) { int off = offset; char val[] = value; int len = count;

Implement an immutable deque as a balanced binary tree?

三世轮回 提交于 2019-11-27 00:10:09
问题 I've been thinking for a while about how to go about implementing a deque (that is, a double-ended queue) as an immutable data structure. There seem to be different ways of doing this. AFAIK, immutable data structures are generally hierarchical, so that major parts of it can be reused after modifying operations such as the insertion or removal of an item. Eric Lippert has two articles on his blog about this topic, along with sample implementations in C#. Both of his implementations strike me