immutability

How to make Scala's immutable collections hold immutable objects

大兔子大兔子 提交于 2019-12-05 06:01:05
I'm evaluating Scala and am having a problem with its immutable collections. I want to make immutable collections, which are completely immutable, right down through all the contained objects, the objects they reference, ad infinitum. Is there a simple way to do this? The code on http://www.finalcog.com/immutable-containers-scala illustrates what I'm trying to achieve, and a nasty work around (ImmutablePoint). The problem with the workaround is that every time I want to change an object I have to manually make a new copy. I understand that the runtime will have to implement copy-on-write, but

How do I modify a record in erlang?

孤街浪徒 提交于 2019-12-05 05:12:32
I to need modify the values {place} and {other_place} in the op record. #op{ action = [walk, from, {place}, to, {other_place}], preconds = [[at, {place}, me], [on, floor, me], [other_place, {place}, {other_place}]], add_list = [[at, {other_place}, me]], del_list = [[at, {place}, me]] } But erlang don´t allow modifying variables. Is there a data type for that? erlang doesn't let you modify variables it is true. But nothing prevents you from making modified copies of a variable. Given your record: Rec = #op{ action = [walk, from, {place}, to, {other_place}], preconds = [[at, {place}, me], [on,

Is hash code of java.lang.String really cached?

耗尽温柔 提交于 2019-12-05 04:44:06
String s1 = "String1"; System.out.println(s1.hashCode()); // return an integer i1 Field field = String.class.getDeclaredField("value"); field.setAccessible(true); char[] value = (char[])field.get(s1); value[0] = 'J'; value[1] = 'a'; value[2] = 'v'; value[3] = 'a'; value[4] = '1'; System.out.println(s1.hashCode()); // return same value of integer i1 Here even after I changed the characters with the help of reflection, same hash code value is mainatained. Is there anything I need to know here? Sotirios Delimanolis A String is meant to be immutable. As such, there is no point having to

Mapping an object to an immutable object with builder (using immutables annotation processor) in mapstruct

被刻印的时光 ゝ 提交于 2019-12-05 03:50:37
We are using the immutables framework to generate all DTOs. Now we would like to map these objects one to another with mapstruct . But the generated DTOs are immutable and have no setters and no constructor, corresponding to the builder pattern. They are only filled through the corresponding builder accessed by a static builder() -method. We instead tried to map DTO1 to DTO2.Builder which would work if mapstruct would recognize the setter in the Builder but these do not have void return type but return the Builder itself for fluent concatenation. So here is the code of the example. We have two

Equality of instance of functional interface in java [duplicate]

偶尔善良 提交于 2019-12-05 03:32:21
This question already has answers here : Is there a way to compare lambdas? (3 answers) Closed 4 years ago . I am not sure how I can be sure about equality/immutability of functional interface. I guess there might be no way to assure equality when I use this syntactic sugar in java 8, please let me know any hint if you have any. I made a short code snippet for my question. public interface Element { void doSomething(int a); } and I've tried to add instance of this interface in functional way public class FunctionSet { public void doubleUp(int a) { System.out.println(a*2); } public void square

Is this a valid Java implementation of an immutable class and the Builder pattern?

坚强是说给别人听的谎言 提交于 2019-12-05 02:51:23
The Builder implements Cloneable and overrides clone() and instead of copying every field of the builder, the immutable class keeps a private clone of the builder. This makes it easy to return a new builder and create slightly modified copies of an immutable instance. This way I can go MyImmutable i1 = new MyImmutable.Builder().foo(1).bar(2).build(); MyImmutable i2 = i1.builder().foo(3).build(); The Cloneable interface is said to be somewhat broken, but does any of this violate good java coding practice, are there any problems with this construct? final class MyImmutable { public int foo() {

What is the cleanest way to remove an element from an immutable array in JS? [duplicate]

眉间皱痕 提交于 2019-12-05 02:44:45
This question already has answers here : Is this the correct way to delete an item using redux? (4 answers) Closed 2 years ago . I need to remove an element from an array that is a state of a React Component. Which means that it is an immutable object. Adding a element is easy using spread syntax. return { ...state, locations: [...state.locations, {}] }; Removing is a little more tricky. I need to use an intermediate object. var l = [...state.locations] l.splice(index, 1) return { ...state, locations: l } It make the code more dirt and difficult to understand. Is there an easier or less tricky

Caching strategy for small immutable objects in Java?

风格不统一 提交于 2019-12-05 02:31:29
I am developing an app that creates a large number of small, immutable Java objects. An example might be: public class Point { final int x; final int y; final int z; ..... } Where it is likely that many instances of Point will need to refer to the same (x,y,z) location. To what extent does it make sense to try to cache and re-use such objects during the lifetime of the application? Any special tricks to handle this kind of situation? The problem you are likely to have is making the object pool light weight enough to be cheaper than just creating the objects. You want to the pool to be large

Understanding immutable composite types with fields of mutable types in Julia

爷,独闯天下 提交于 2019-12-05 02:10:22
Initial note: I'm working in Julia, but this question probably applies to many languages. Setup: I have a composite type as follows: type MyType x::Vector{String} end I write some methods to act on MyType . For example, I write a method that allows me to insert a new element in x , e.g. function insert!(d::MyType, itemToInsert::String) . Question: Should MyType be mutable or immutable? My understanding: I've read the Julia docs on this, as well as more general (and highly upvoted) questions on Stackoverflow (e.g. here or here ), but I still don't really have a good handle on what it means to

Is a constexpr more “constant” than const?

梦想的初衷 提交于 2019-12-05 00:41:09
The C++ Programming Language Fourth Edition - Bjarne Stroustrup: (emphasis mine) 2.2.3. Constants In a few places, constant expressions are required by language rules (e.g., array bounds (§2.2.5, §7.3), case labels (§2.2.4, §9.4.2), some template arguments (§25.2), and constants declared using constexpr). In other cases, compile-time evaluation is important for performance. Independently of performance issues, the notion of immutability (of an object with an unchangeable state) is an important design concern (§10.4). It seems that Stroustrup is suggesting here that constexpr ensures