immutability

Are open(file, “wt” or “rt”) different objects?

耗尽温柔 提交于 2020-01-15 05:29:07
问题 When you do: file = open("my file","wt") and file = open("my file" , "rt") These both create file objects that we use file methods on. But are they creating different file objects? And if they are creating different file objects would it be fair to say that the "wt" one is mutable, while the "rt" one is immutable? 回答1: No, that would not be fair to say. You are creating instances of the same standard file type, which proxies file manipulation calls to the operating system. The mode defines

How is state immutability actually used in Redux?

感情迁移 提交于 2020-01-14 19:23:49
问题 I am trying to understand how immutability is actually (if at all) used in Redux. Every tutorial/article/document I found states that reducers should never alter the state object but instead create a new copy of the changed data (i.e. the reducers must be pure functions). I understand this but I could not find any place where there is an explanation of how this guideline is actually used by Redux internal implementation. Is this just a strong recomendation or will something inside Redux

Java immutable-class rules

冷暖自知 提交于 2020-01-14 10:45:50
问题 Is the below class immutable: final class MyClass { private final int[] array; public MyClass(int[] array){ this.array = array; } } 回答1: No it is not because the elements of the array can still be changed. int[] v1 = new int[10]; MyClass v2 = new MyClass(v1); v1[0] = 42; // mutation visible to MyClass1 回答2: My two cents regarding immutability rules (which I retained from reading Effective Java - a great book!): Don't provide methods that can modify the state of an object. Make all your fields

Updating large data structures in idiomatic Scala

烈酒焚心 提交于 2020-01-13 18:31:08
问题 I've been experimenting with Scala for some time, and have often encountered the advice to favor immutable data structures. But when you have a data structure like e.g. a 3D scene graph, a large neural network, or anything with quite a few objects that need frequent updates (animating the objects in the scene, training the neural net, ...), this seems to be horribly inefficient at runtime since you need to constantly recreate the whole object graph, and difficult to program since when you

How do I modify a record in erlang?

你。 提交于 2020-01-13 08:35:08
问题 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? 回答1: erlang doesn't let you modify variables it is true. But nothing prevents you from making modified copies of a variable. Given your

Java Immutable Objects [closed]

守給你的承諾、 提交于 2020-01-12 19:02:06
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I am learning the concept of immutability. I understand that immutable objects cannot change their values once the object is created. But I didn't understand the following uses of immutable objects. They are are automatically thread-safe and have no synchronization issues. How ?

.NET ORMs, immutable value objects, structs, default constructors, and readonly properties

喜欢而已 提交于 2020-01-12 18:47:20
问题 I am just getting started with .NET ORMs, to the point where I haven't even decided between Entity Framework and NHibernate. But in both cases, I'm running into a problem in that they seem to want me to compromise the integrity of my domain model in various ways, especially on finer points of C# object design. This is one of several questions on the subject. I am very used to enforcing immutability on appropriate properties with a pattern that looks like this: public class Foo { private

.NET ORMs, immutable value objects, structs, default constructors, and readonly properties

末鹿安然 提交于 2020-01-12 18:47:13
问题 I am just getting started with .NET ORMs, to the point where I haven't even decided between Entity Framework and NHibernate. But in both cases, I'm running into a problem in that they seem to want me to compromise the integrity of my domain model in various ways, especially on finer points of C# object design. This is one of several questions on the subject. I am very used to enforcing immutability on appropriate properties with a pattern that looks like this: public class Foo { private

Functional way to implement a thread safe shared counter

一世执手 提交于 2020-01-12 08:02:11
问题 I'm relatively new to Scala and functional programming, and I like the idea that using immutable objects I can avoid many thread safety pitfalls. One thing still haunts me, and it's the classical example used to teach thread safety - the shared counter. I was wondering if it would be possible to implement a thread-safe counter (a request counter in this example), using immutable objects, and functional concepts, and avoid synchronization completely. So for reference here are first the

F# pattern match directly against let binding

倾然丶 夕夏残阳落幕 提交于 2020-01-11 10:04:34
问题 Is it possible in F# to pattern match directly against a let binding? For example, this compiles without any warnings: let value = match arg with | 1 -> "value1" | 2 -> "value2" | _ -> failwith "key not found" Whereas the following gives the warning "This rule will never be matched" against the lines matching key2 and _ : let key1 = 1 let key2 = 2 let value = match arg with | key1 -> "value1" | key2 -> "value2" | _ -> failwith "key not found" Is this because although they're immutable, the