immutability

F# pattern match directly against let binding

痴心易碎 提交于 2019-12-01 20:33:49
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 let bindings are unlike C# const variables? just use capital letters and [<Literal>] them and it works as

Update nested data arrays of object (Redux)

一笑奈何 提交于 2019-12-01 19:48:34
I have an issue with updating the immutable redux and quite nested data. Here's an example of my data structure and what I want to change. If anyone could show me the pattern of accessing this update using ES6 and spread operator I would be thankful. 😀 const formCanvasInit = { id: guid(), fieldRow: [{ id: guid(), fieldGroup: [ { type: 'text', inputFocused: true }, // I want to change inputFocused value { type: 'text', inputFocused: false }, ], }], // ... }; This should do the trick, assuming the data is set up exactly as shown, with the given array indices: const newData = { ...formCanvasInit,

how do I increment an integer variable I passed into a function in Scala?

别说谁变了你拦得住时间么 提交于 2019-12-01 18:14:39
I declared a variable outside the function like this: var s: Int = 0 passed it such as this: def function(s: Int): Boolean={ s += 1 return true } but the error lines wont go away under the "s +=" for the life of me. I tried everything. I am new to Scala btw. First of all, I will repeat my words of caution: solution below is both obscure and inefficient , if it possible try to stick with val ues. implicit class MutableInt(var value: Int) { def inc() = { value+=1 } } def function(s: MutableInt): Boolean={ s.inc() // parentheses here to denote that method has side effects return true } And here

How to make an immutable singleton in Java?

耗尽温柔 提交于 2019-12-01 18:03:14
An immutable object is initialized by its constuctor only, while a singleton is instantiated by a static method. How to make an immutable singleton in Java? while a singleton is instantiated by a static method While this is the usual way of doing it, this is by no means the only way. In Java 1.5 a new version of Singleton is the enum singleton pattern: public enum Elvis{ INSTANCE // this is a singleton, no static methods involved } And since enums can have constructors, methods and fields, you can give them all the immutable state you want. Reference: Java tutorial: Enum Types Effective Java ,

Copy array of objects and make changes without modifying original array

谁说胖子不能爱 提交于 2019-12-01 17:16:28
问题 I have an array of objects. I would like to deep copy the array of objects and make some changes to each object. I want to do this without modifying the original array or original objects that were in that array. This is the way I have done it. However, being new to JavaScript I want to make sure this is a good approach. Is there a better way to do this? const users = [ { id : 1, name : 'Jack', approved : false }, { id : 2, name : 'Bill', approved : true }, { id : 3, name : 'Rick', approved :

How thread safe are immutable objects?

≡放荡痞女 提交于 2019-12-01 17:00:02
问题 Everybody says that immutable objects are thread safe, but why is this? Take the following scenario running on a multi core CPU: Core 1 reads an object at memory location 0x100 and it is cached in the L1/L2 cache of Core 1; The GC collects this object at that memory location because it has become eligible and 0x100 becomes available for new objects; Core 2 allocates an (immutable) object which is located at address 0x100 ; Core 1 gets a reference to this new object and reads it at memory

How thread safe are immutable objects?

浪尽此生 提交于 2019-12-01 16:36:16
Everybody says that immutable objects are thread safe, but why is this? Take the following scenario running on a multi core CPU: Core 1 reads an object at memory location 0x100 and it is cached in the L1/L2 cache of Core 1; The GC collects this object at that memory location because it has become eligible and 0x100 becomes available for new objects; Core 2 allocates an (immutable) object which is located at address 0x100 ; Core 1 gets a reference to this new object and reads it at memory location 0x100 . In this situation, when Core 1 asks for the value at location 0x100 is it possible that it

Ruby - Immutable Objects

你说的曾经没有我的故事 提交于 2019-12-01 16:08:27
I've got a highly multithreaded app written in Ruby that shares a few instance variables. Writes to these variables are rare (1%) while reads are very common (99%). What is the best way (either in your opinion or in the idiomatic Ruby fashion) to ensure that these threads always see the most up-to-date values involved? Here's some ideas so far that I had (although I'd like your input before I overhaul this): Have a lock that most be used before reading or writing any of these variables (from Java Concurrency in Practice ). The downside of this is that it puts a lot of synchronize blocks in my

How do I create a Vec from a range and shuffle it?

情到浓时终转凉″ 提交于 2019-12-01 15:39:37
I have the following code: extern crate rand; use rand::{thread_rng, Rng}; fn main() { let mut vec: Vec<u32> = (0..10).collect(); let mut slice: &[u32] = vec.as_mut_slice(); thread_rng().shuffle(slice); } and get the following error: error[E0308]: mismatched types --> src/main.rs:9:26 | 9 | thread_rng().shuffle(slice); | ^^^^^ types differ in mutability | = note: expected type `&mut [_]` found type `&[u32]` I think I understand that the content of vectors and slices is immutable and that causes the error here, but I'm unsure. The signature of as_mut_slice is pub fn as_mut_slice<'a>(&'a mut

The CA2104 warning: Is there any way to mark a class as `Immutable` to suppress it?

倾然丶 夕夏残阳落幕 提交于 2019-12-01 15:21:56
Consider the following code, which provokes CA2104: Do not declare read only mutable reference types. public class Test { // This provokes CA2104: "Do not declare read only mutable reference types". protected readonly ImmutableClass ImmutableMember; } public class ImmutableClass { } Does anyone know of a way to mark a class as immutable in a way that would suppress warning CA2104? I tried decorating MutableClass with [ImmutableObject(true)] with no hope of success (since that attribute is pretty clearly for the Form Editor to use), and sure enough it doesn't work. I assume that Code Analysis