immutability

Unexpected Behavior of Extend with a list in Python [duplicate]

心不动则不痛 提交于 2019-11-28 11:57:26
This question already has an answer here: Why does list.append evaluate to false in a boolean context? [duplicate] 7 answers I am trying to understand how extend works in Python and it is not quite doing what I would expect. For instance: >>> a = [1, 2, 3] >>> b = [4, 5, 6].extend(a) >>> b >>> But I would have expected: [4, 5, 6, 1, 2, 3] Why is that returning a None instead of extending the list? The extend() method appends to the existing array and returns None . In your case, you are creating an array — [4, 5, 6] — on the fly, extending it and then discarding it. The variable b ends up with

Immutable objects and unmodifiable collections

本小妞迷上赌 提交于 2019-11-28 10:07:25
问题 With an immutable object, where is it proper to wrap a contained collection as unmodifiable? I see 3 options: In the immutable object's factory: public class ImmutableFactory { public Immutable build(){ List<Integer> values = new ArrayList<Integer>(); values.add(1); values.add(2); values.add(3); return new Immutable(Collections.unmodifiableList(values), "hello"); } } In the immutable's contructor public class Immutable { private final List<Integer> values; private final String hello; public

Deserializing ImmutableList using Gson

一世执手 提交于 2019-11-28 10:02:12
I'm using quite a few immutable collections and I'm curious how to deserialize them using Gson. As nobody answered and I've found the solution myself, I'm simplifying the question and presenting my own answer. I had two problems: How to write a single Deserializer working for all ImmutableList<XXX> ? How to register it for all ImmutableList<XXX> ? Update: There's https://github.com/acebaggins/gson-serializers which covers many guava collections: ImmutableList ImmutableSet ImmutableSortedSet ImmutableMap ImmutableSortedMap How to write a single Deserializer working for all ImmutableList? The

Swift: What's the best way to pair up elements of an Array

不想你离开。 提交于 2019-11-28 09:26:13
I came across a problem that required iterating over an array in pairs. What's the best way to do this? Or, as an alternative, what's the best way of transforming an Array into an Array of pairs (which could then be iterated normally)? Here's the best I got. It requires output to be a var , and it's not really pretty. Is there a better way? let input = [1, 2, 3, 4, 5, 6] var output = [(Int, Int)]() for i in stride(from: 0, to: input.count - 1, by: 2) { output.append((input[i], input[i+1])) } print(output) // [(1, 2), (3, 4), (5, 6)] // let desiredOutput = [(1, 2), (3, 4), (5, 6)] // print

JavaScript const Keyword

孤街浪徒 提交于 2019-11-28 09:23:45
Does the const keyword in JavaScript create an immutable reference to immutable data structures? [I'm assuming that immutable data structures exist in JavaScript.] For string it appears to do so: var x = "asdf"; const constantX = x; alert("before mutation: " + constantX); x = "mutated" alert("after mutation: " + constantX); output: before mutation: asdf after mutation: asdf http://jsfiddle.net/hVJ2a/ Yes, it does create an immutable reference, but it is not been standardized and is not supported in all browsers. See this MDN article on the const keyword for details and compatability. However,

How can we maintain Immutability of a class with a mutable reference

拈花ヽ惹草 提交于 2019-11-28 07:45:49
I know all the basic rules to make our class immutable but I am a little confused when there is another class reference. I know if there is collection instead of Address then we can make use of Collections.unmodifiableList(new ArrayList<>(modifiable)); and then we can make our class immutable. But in below case I am still unable to get the concept. public final class Employee{ private final int id; private Address address; public Employee(int id, Address address) { this.id = id; this.address=address; } public int getId(){ return id; } public Address getAddress(){ return address; } } public

making a class immutable in java

寵の児 提交于 2019-11-28 07:39:35
问题 To make a class immutable what I can do is: 1)Make class final 2)do not provide setters 3)mark all variables as final But if my class has another object of some other class then , somone can change value of that object class MyClass{ final int a; final OtherClass other MyClass(int a ,OtherClass other){ this.a = a; this.other = other; } int getA(){ return a; } OtherClass getOther(){ return other; } public static void main(String ags[]){ MyClass m = new Myclass(1,new OtherClass); Other o = m

How to make a copy of a reference type [duplicate]

纵然是瞬间 提交于 2019-11-28 07:37:29
问题 Possible Duplicate: Cloning objects in C# I have a class with properties and some of them are reference types (instances of other classes) themselves ( CookieContainer ). I want to have a exact copy of this class so any change to previous version would not affect this new instance. Is there a general solution for this kind of problems or I should do it manually ? 回答1: You need to deep copy the object to another object. There are many approaches to this but serializing one object and

Pandas: Modify a particular level of Multiindex

泄露秘密 提交于 2019-11-28 07:27:32
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 is a more elegant way to modify one particular level of the Multiindex directly. Thanks to @cxrodgers's

Mutability of string when string doesn't change in C#

隐身守侯 提交于 2019-11-28 07:05:22
问题 If the string operation doesn't change the value of string, will that end up in creation of a new instance? For example, string str = "foo"; str += ""; I know the difference between string and StringBuilder in C#. 回答1: No, a new instance will be created only when the string operation changes the value in the string variable. It can be proved using the ObjectIDGenerator class. It's worth reading this complete article for proof. using System; using System.Text; using System.Runtime