immutability

String immutability in C#

丶灬走出姿态 提交于 2019-12-17 22:22:43
问题 I was curious how the StringBuilder class is implemented internally, so I decided to check out Mono's source code and compare it with Reflector's disassembled code of the Microsoft's implementation. Essentially, Microsoft's implementation uses char[] to store a string representation internally, and a bunch of unsafe methods to manipulate it. This is straightforward and did not raise any questions. But I was confused, when I found that Mono uses a string inside StringBuilder: private int

Why must dictionary keys be immutable?

℡╲_俬逩灬. 提交于 2019-12-17 20:06:13
问题 Why is it necessary for dictionary keys to be immutable? I'm looking for a simple, clear reason why keys in Python dictionaries have that restriction. 回答1: On my computer, there's a file /etc/dictionaries-common/words containing a large collection of English words: >>> with open("/etc/dictionaries-common/words") as f: ... words = [line.strip() for line in f] ... >>> "python" in words True >>> "BDFL" in words False Let's create a dictionary storing the lengths of all those words: >>> word

Why should dictionary keys be immutable?

不想你离开。 提交于 2019-12-17 19:55:31
问题 Got a question about why they ask to use immutable objects as keys in a Dictionary. The question actually got in my head when I recently used a dictionary (not for the very purpose of a Hash table apparently though) to place Xml Node objects as keys. I then updated the nodes several times during the usage. So what does 'use immutable keys' really mean? 回答1: When you insert a key into a hash table, the hash table asks the key for its hash code, and remembers it along with the key itself and

Efficiently carry out multiple string replacements in Python

点点圈 提交于 2019-12-17 18:45:26
问题 If I would like to carry out multiple string replacements, what is the most efficient way to carry this out? An example of the kind of situation I have encountered in my travels is as follows: >>> strings = ['a', 'list', 'of', 'strings'] >>> [s.replace('a', '')...replace('u', '') for s in strings if len(s) > 2] ['a', 'lst', 'of', 'strngs'] 回答1: The specific example you give (deleting single characters) is perfect for the translate method of strings, as is substitution of single characters

immutable strings vs std::string

别说谁变了你拦得住时间么 提交于 2019-12-17 17:36:59
问题 I've recent been reading about immutable strings Why can't strings be mutable in Java and .NET? and Why .NET String is immutable? as well some stuff about why D chose immutable strings. There seem to be many advantages. trivially thread safe more secure more memory efficient in most use cases. cheap substrings (tokenizing and slicing) Not to mention most new languages have immutable strings, D2.0, Java, C#, Python, etc. Would C++ benefit from immutable strings? Is it possible to implement an

Immutable collections?

岁酱吖の 提交于 2019-12-17 17:30:05
问题 I am making most of my basic types in my app, immutable. But should the collections be immutable too? To me, this seems like a huge overhead unless I am missing something. I am talking about collections to hold Point3 values, etc which can be added as it goes at different times. So if there are 1M values in a collection, and you needed to delete 1 of them, you would have to create the same collection all over again, right? 回答1: Eric Lippert has a series on Immutability in C#, and if you read

Javascript: Is the length method efficient?

馋奶兔 提交于 2019-12-17 16:10:02
问题 i'm doing some javascript coding and I was wondering if the length method is "precomputed", or remembered by the JS engine. So, the question is: If I'm checking really often for an array length, and supposing that i'm not changing it (making it immutable through a closure), should I precompute the length method and store it in some variable? Thanks! 回答1: All major interpreters provide efficient accessors for the lengths of native arrays, but not for array-like objects like NodeLists.

Mutable objects and hashCode

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 09:33:39
问题 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

Why are Python strings immutable? Best practices for using them

别说谁变了你拦得住时间么 提交于 2019-12-17 07:17:10
问题 What are the design reasons of making Python strings immutable? How does it make programming easier? I'm used to mutable strings, like the ones in C. How am I supposed to program without mutable strings? Are there any best practices? 回答1: When you receive a string, you'll be sure that it stays the same. Suppose that you'd construct a Foo as below with a string argument, and would then modify the string; then the Foo 's name would suddenly change: class Foo(object): def __init__(self, name):

Allen Holub wrote “You should never use get/set functions”, is he correct? [duplicate]

半城伤御伤魂 提交于 2019-12-17 05:40:21
问题 This question already has answers here : Why use getters and setters/accessors? (38 answers) Closed 4 years ago . Allen Holub wrote the following, You can't have a program without some coupling. Nonetheless, you can minimize coupling considerably by slavishly following OO (object-oriented) precepts (the most important is that the implementation of an object should be completely hidden from the objects that use it). For example, an object's instance variables (member fields that aren't