immutability

How can one create cyclic (and immutable) data structures in Clojure without extra indirection?

江枫思渺然 提交于 2019-12-03 00:25:38
I need to represent directed graphs in Clojure. I'd like to represent each node in the graph as an object (probably a record) that includes a field called :edges that is a collection of the nodes that are directly reachable from the current node. Hopefully it goes without saying, but I would like these graphs to be immutable. I can construct directed acyclic graphs with this approach as long as I do a topological sort and build each graph "from the leaves up". This approach doesn't work for cyclic graphs, however. The one workaround I can think of is to have a separate collection (probably a

Remove a property in an object immutably

不羁岁月 提交于 2019-12-03 00:22:12
问题 I am using Redux. In my reducer I'm trying to remove a property from an object like this: const state = { a: '1', b: '2', c: { x: '42', y: '43' }, } And I want to have something like this without having to mutate the original state: const newState = { a: '1', b: '2', c: { x: '42', }, } I tried: let newState = Object.assign({}, state); delete newState.c.y but for some reasons, it deletes the property from both states. Could help me to do that? 回答1: How about using destructuring assignment

Best way to separate read and write concerns using interfaces?

陌路散爱 提交于 2019-12-03 00:20:18
Lately I've been realizing the benefit of (some would argue overuse of) immutable objects to cut down dramatically on read-write dependency issues in my object model and their resulting conditions and side-effects, to ultimately make the code simpler to manage (kind of functional-programming-esque). This practice has led me to create read-only objects that are provided values at creation/construction time and then to make available only public getters for external callers to access the properties with. Protected, internal and private setters allow internal control to be maintained over writing

How to deal with the immutability of returned structs?

谁说我不能喝 提交于 2019-12-03 00:15:36
I'm writing a game that has a huge 2D array of "cells". A cell takes only 3 bytes. I also have a class called CellMap, which contains the 2D array as a private field, and provides access to it via a public indexer. Profiling showed that a performance problem is caused by garbage collection of too many Cell objects. So I decided to make Cell a struct (it was a class). But now code like this doesn't work: cellMap[x, y].Population++; I can think of many options, but I don't really like any of them. Make the array public , and write cellMap.Data[x, y].Population = 5; Stop using a CellMap class ,

C#, immutability and public readonly fields

拜拜、爱过 提交于 2019-12-02 23:26:38
I have read in many places that exposing fields publicly is not a good idea, because if you later want to change to properties, you will have to recompile all the code which uses your class. However, in the case of immutable classes, I don't see why you would ever need to change to properties - you're not going to be adding logic to the 'set' after all. Any thoughts on this, am I missing something? Example of the difference, for those who read code more easily than text :) //Immutable Tuple using public readonly fields public class Tuple<T1,T2> { public readonly T1 Item1; public readonly T2

Update one of the objects in array, in an immutable way

戏子无情 提交于 2019-12-02 22:45:38
In React's this.state I have a property called formErrors containing the following dynamic array of objects. [ {fieldName: 'title', valid: false}, {fieldName: 'description', valid: true}, {fieldName: 'cityId', valid: false}, {fieldName: 'hostDescription', valid: false}, ] Let's say I would need to update state's object having the fieldName cityId to the valid value of true . What's the easiest or most common way to solve this? I'm OK to use any of the libraries immutability-helper , immutable-js etc or ES6. I've tried and googled this for over 4 hours, and still cannot wrap my head around it.

How to design an immutable object with complex initialization

☆樱花仙子☆ 提交于 2019-12-02 22:08:10
I'm learning about DDD, and have come across the statement that "value-objects" should be immutable. I understand that this means that the objects state should not change after it has been created. This is kind of a new way of thinking for me, but it makes sense in many cases. Ok, so I start creating immutable value-objects. I make sure they take the entire state as parameters to the constructor, I don't add property setters, and make sure no methods are allowed to modify the content (only return new instances). But now I want to create this value object that will contain 8 different numeric

Extending Python's builtin Str

我只是一个虾纸丫 提交于 2019-12-02 21:58:08
I'm trying to subclass str , but having some difficulties due to its immutability. class DerivedClass(str): def __new__(cls, string): ob = super(DerivedClass, cls).__new__(cls, string) return ob def upper(self): #overridden, new functionality. Return ob of type DerivedClass. Great. caps = super(DerivedClass, self).upper() return DerivedClass(caps + '123') derived = DerivedClass('a') print derived.upper() #'A123' print type(derived.upper()) #<class '__main__.DerivedClass'> print derived.lower() #'a' print type(derived.lower()) #<type 'str'> For inherited methods that don't require any new

Parsing nested Records in Immutable.js

我的梦境 提交于 2019-12-02 20:48:14
Suppose I have the following Records defined using Immutable.js: var Address = Immutable.Record({street: '', city: '', zip: ''}); var User = Immutable.Record({name: '', address: new Address()}); How do I convert plain javascript object into the User record? I tried the following but it does not produce the expected output: var user = new User({name: 'Foo', address: {street: 'Bar', city: 'Baz'}}); // => Record { "name": "Foo", "address": [object Object] } I am aware that it is possible to explicitly create the Address record: var user = new User({name: 'Foo', address: new Address({street: 'Bar'

Strings vs classes when both are reference types

落花浮王杯 提交于 2019-12-02 20:42:03
Here is how my last interview went: Question: Where are strings stored? Answer: Heap since it is a reference type Question: Explain me this following code: static void Main(string[] args) { string one = "test"; string two = one; one = one + " string"; Console.WriteLine("One is {0}" , one); Console.WriteLine("Two is {0}", two); } Answer: Drew two diagrams like below: (represents the statement, string two = one; (represents the statement, one = one + " string"; . A new string is created on heap and assigned) Question: Correct. Draw similar for the code snippet below: class Program { static void