immutability

Are some data structures more suitable for functional programming than others?

家住魔仙堡 提交于 2019-12-03 04:19:13
问题 In Real World Haskell, there is a section titled "Life without arrays or hash tables" where the authors suggest that list and trees are preferred in functional programming, whereas an array or a hash table might be used instead in an imperative program. This makes sense, since it's much easier to reuse part of an (immutable) list or tree when creating a new one than to do so with an array. So my questions are: Are there really significantly different usage patterns for data structures between

Use frozenset as a pair in python

爱⌒轻易说出口 提交于 2019-12-03 04:11:12
I would like to make a pair of two elements. I don't care about the order of the elements, so I use frozenset . I can think of the following two methods to iterate the elements back from the frozenset. Isn't there any fancier method? Thanks in advance. pair = frozenset([element1, element2]) pair2 = list(pair) elem1 = pair2[0] elem2 = pair2[1] pair = frozenset([element1, element2]) elems = [] for elem in pair: elems.append(elem) elem1 = elems[0] elem2 = elems[1] pair = frozenset([element1, element2]) elem1, elem2 = pair If you have a lot of those pair things, using frozenset() is NOT a good

Constant Object vs Immutable Object

偶尔善良 提交于 2019-12-03 03:03:33
Can I use the term "Constant Object" in the place of the term "Immutable Object"? Though I get the feeling that Immutable for an Object is what Constant is for a variable, I am not sure if this terminology is accepted. Please help me understand. Thanks, Karthick S. In fact, in Java the term constant has no defined meaning. It occurs in the JLS only in the larger term compile time constant expression , which is an expression which can (and must) be calculated by the compiler instead of at runtime. (And the keyword const is reserved to allow compilers to give better error messages.) In Java, we

Value-based Classes confusion

拜拜、爱过 提交于 2019-12-03 02:35:55
I'm seeking some clarification to the definition of Value-based Classes . I can't imagine, how is the last bullet point (6) supposed to work together with the first one (1) they are final and immutable ( though may contain references to mutable objects ) (6) they are freely substitutable when equal, meaning that interchanging any two instances x and y that are equal according to equals() in any computation or method invocation should produce no visible change in behavior. Optional is such a class. Optional a = Optional.of(new ArrayList<String>()); Optional b = Optional.of(new ArrayList<String>

If delegates are immutable, why can I do things like x += y?

对着背影说爱祢 提交于 2019-12-03 02:32:17
Reading C# In Depth, 2nd edition , section 2.1.2 on combining and removing delegates. The subsection title states that "delegates are immutable" and that "nothing about them can be changed." In the next paragraph, though, it talks about using constructs like x += y; where x and y are variables of compatible delegate types. Didn't I just change x ? Or does the immutability part deal with when x is disposed of when I do this (i.e., immediately)? That's like doing: string x = "x"; string y = "y"; x += y; Strings are immutable too. The code above not changing the string objects - it's setting x to

Immutable Object with ArrayList member variable - why can this variable be changed?

半世苍凉 提交于 2019-12-03 02:25:48
问题 I have got one class with various member variables. There is a constructor and there are getter-methods, but no setter-methods. In fact, this object should be immutable. public class Example { private ArrayList<String> list; } Now I noticed the following: when I get the variable list with a getter-method, I can add new values and so on - I can change the ArrayList . When I call the next time get() for this variable, the changed ArrayList is returned. How can this be? I didn't set it again, I

Possible to have immutable JPA entities?

穿精又带淫゛_ 提交于 2019-12-03 02:02:10
In our hibernate project, the entities are coded using the java beans pattern. There's quite a few spots in our code where someone has forgotten a to set a mutator and we get an exception due to a NOT NULL field. Is anyone using a builder to construct their entities or making them immutable? I'm trying to find an effective pattern that is not in the style of the java beans pattern. Thanks If you make your beans immutable then you have to use field level access and this comes with its own set of problems as discussed thoroughly here . The approach we took is to have a Builder/Factory enforcing

Set System.Drawing.Color values

眉间皱痕 提交于 2019-12-03 01:58:17
Hi how to set R G B values in System.Drawing.Color.G ? which is like System.Drawing.Color.G=255; is not allowed because its read only Property or indexer 'System.Drawing.Color.G' cannot be assigned toit is read only i just need to create a Color Object by assigning custom R G B values You could create a color using the static FromArgb method: Color redColor = Color.FromArgb(255, 0, 0); You can also specify the alpha using the following overload . The Color structure is immutable (as all structures should really be), meaning that the values of its properties cannot be changed once that

How do I delete specific characters from a particular String in Java?

北城以北 提交于 2019-12-03 00:58:18
For example I'm extracting a text String from a text file and I need those words to form an array. However, when I do all that some words end with comma (,) or a full stop (.) or even have brackets attached to them (which is all perfectly normal). What I want to do is to get rid of those characters. I've been trying to do that using those predefined String methods in Java but I just can't get around it. OMG Ponies Use: String str = "whatever"; str = str.replaceAll("[,.]", ""); replaceAll takes a regular expression . This: [,.] ...looks for each comma and/or period. Reassign the variable to a

Persistent data structures in Java

戏子无情 提交于 2019-12-03 00:57:03
问题 Does anyone know a library or some at least some research on creating and using persistent data structures in Java? I don't refer to persistence as long term storage but persistence in terms of immutability (see Wikipedia entry). I'm currently exploring different ways to model an api for persistent structures. Using builders seems to be a interesting solution: // create persistent instance Person p = Builder.create(Person.class) .withName("Joe") .withAddress(Builder.create(Address.class)