immutability

Correct way to push into state array

不问归期 提交于 2019-11-27 10:43:05
I seem to be having issues pushing data into a state array. I am trying to achieve it this way: this.setState({ myArray: this.state.myArray.push('new value') }) But I believe this is incorrect way and causes issues with mutability? Array push returns length this.state.myArray.push('new value') returns the length of the extended array, instead of the array itself. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push I guess you expect the returned value to be the array. Immutability It seems it's rather the behaviour of React: NEVER mutate this.state

Downsides to immutable objects in Java? [closed]

∥☆過路亽.° 提交于 2019-11-27 10:30:09
问题 The advantages of immutable objects in Java seem clear: consistent state automatic thread safety simplicity You can favour immutability by using private final fields and constructor injection. But, what are the downsides to favouring immutable objects in Java? i.e. incompatibility with ORM or web presentation tools? Inflexible design? Implementation complexities? Is it possible to design a large-scale system (deep object graph) that predominately uses immutable objects? 回答1: But, what are the

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

◇◆丶佛笑我妖孽 提交于 2019-11-27 10:27:30
问题 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? 回答1: 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

Is “new String()” immutable as well?

不打扰是莪最后的温柔 提交于 2019-11-27 09:55:26
问题 I've been studying Java String for a while. The following questions are based on the below posts Java String is special Immutability of String in java Immutability: Now, going by the immutability, the String class has been designed so that the values in the common pool can be reused in other places/variables. This holds good if the String was created as String a = "Hello World!"; However, if I create String like String b = new String("Hello World!"); why is this immutable as well? (or is it?)

What is the data structure behind Clojure's sets?

半腔热情 提交于 2019-11-27 09:47:33
问题 I recently listened to Rich Hickey's interview on Software Engineering Radio. During the interview Rich mentioned that Clojure's collections are implemented as trees. I'm hoping to implement persistent data structures in another language, and would like to understand how sets and Clojure's other persistent data structures are implemented. What would the tree look like at each point in the following scenario? Create the set {1 2 3} Create the union of {1 2 3} and {4} Create the difference of

Python, subclassing immutable types

梦想的初衷 提交于 2019-11-27 08:55:22
I've the following class: class MySet(set): def __init__(self, arg=None): if isinstance(arg, basestring): arg = arg.split() set.__init__(self, arg) This works as expected (initialising the set with the words of the string rather than the letters). However when I want to do the same with the immutable version of set, the __init__ method seems to be ignored: class MySet(frozenset): def __init__(self, arg=None): if isinstance(arg, basestring): arg = arg.split() frozenset.__init__(self, arg) Can I achieve something similar with __new__ ? Yes, you need to override __new__ special method: class

Why are integers immutable in Python? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 08:54:13
This question already has an answer here: Immutable vs Mutable types 16 answers I understand the differences between mutable and immutable objects in Python. I have read many posts discussing the differences. However, I have not read anything regarding WHY integers are immutable objects. Does there exist a reason for this? Or is the answer "that's just how it is"? Edit: I am getting prompted to 'differentiate' this question from other questions as it seems to be a previously asked question. However, I believe what I'm asking is more of a philosophical Python question rather than a technical

Efficient implementation of immutable (double) LinkedList

拈花ヽ惹草 提交于 2019-11-27 07:55:18
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, how would we ensure immutability? Should we recursively copy all the references to the list when we

Mutable objects and hashCode

≯℡__Kan透↙ 提交于 2019-11-27 07:51:41
Have the following class: public class Member { private int x; private long y; private double d; public Member(int x, long y, double d) { this.x = x; this.y = y; this.d = d; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = (int) (prime * result + y); result = (int) (prime * result + Double.doubleToLongBits(d)); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof Member) { Member other = (Member) obj; return other.x == x && other.y == y && Double.compare(d, other.d) ==

Why does post-increment work on wrapper classes

做~自己de王妃 提交于 2019-11-27 07:45:35
问题 I was doing a review of some code and came across an instance of someone post-incrementing a member variable that was a wrapper class around Integer. I tried it myself and was genuinely surprised that it works. Integer x = 0; System.out.print(x++ + ", "); System.out.print(x); This prints out 0, 1 , not 0, 0 as I would have expected. I've looked through the language specification and can't find anything covering this. Can anyone explain to me why this works and if it's safe across multiple