immutability

Immutability and components props in React native (vs. React)

蹲街弑〆低调 提交于 2019-12-06 06:11:34
This question is somewhat related to How to pass props to {this.props.children} . That may sound like a dumb question which has been answered a million times, but I can't figure it out what is true and what is not from what I've read so far. I can't find detailed information about this topic on either react or react native documentation. So, does anyone knows how props are handled (under the hood) in react native? I'm asking this because I've read in several places either one of: props are immutable components themselves are immutable props/components are not immutable, but changing them will

Design a mutable class that after it's consumed becomes immutable

倖福魔咒の 提交于 2019-12-06 03:33:16
问题 Suppose that the scenario doesn't allow to implement an immutable type. Following that assumption, I'd like opinions / examples on how to properly design a type that after it's consumed, becomes immutable. public class ObjectAConfig { private int _valueB; private string _valueA; internal bool Consumed { get; set; } public int ValueB { get { return _valueB; } set { if (Consumed) throw new InvalidOperationException(); _valueB = value; } } public string ValueA { get { return _valueA; } set { if

Haskell - for loop

杀马特。学长 韩版系。学妹 提交于 2019-12-06 03:18:40
if I want to express something like [just a simple example]: int a = 0; for (int x = 0; x < n; x += 1) a = 1 - a; what should I do in Haskell, since it doesn't have variable concept? (maybe wrong, see: Does Haskell have variables? ) Often, repetition that you would perform with a loop in a procedural language is accomplished with recursion in Haskell. In this case, you should think about what the result of the loop is. It appears to alternate between 0 and 1. There are several ways to do this in Haskell. One way is alternatingList n = take n alternating0and1 alternating0and1 = 0 :

Rewriting state in Redux

Deadly 提交于 2019-12-06 01:07:35
In redux I understand that state is immutable and when you create new state you are essentially updating the object with what ever new information there is and then totally rewriting the state. Today I had a thought and I am not sure how stupid it is. Is it computationally expensive to keep re-writing the state? I know that this is one of the major paradigms of Redux, but I want to know if this makes sense from a memory and space perspective. You are allowed to mutate the state in Redux but you should not do it at any cost because you'd be coding in Redux anti-patterns Mutating objects, in

How do I swap array elements in an immutable fashion within a Redux reducer?

半城伤御伤魂 提交于 2019-12-05 23:38:13
问题 The relevant Redux state consists of an array of objects representing layers. Example: let state = [ { id: 1 }, { id: 2 }, { id: 3 } ] I have a Redux action called moveLayerIndex: actions.js export const moveLayerIndex = (id, destinationIndex) => ({ type: MOVE_LAYER_INDEX, id, destinationIndex }) I would like the reducer to handle the action by swapping the position of the elements in the array. reducers/layers.js const layers = (state=[], action) => { switch(action.type) { case 'MOVE_LAYER

React: Why are props not frozen?

帅比萌擦擦* 提交于 2019-12-05 20:00:15
React suggests not to mutate props. I was operating under the impression that when props are passed in, they would be immutable or frozen. The code below does not throw any errors and mutates the prop. Is this a bug? try{ var a = this.props.a; a.push('one') a.push('two') a.push('three') console.log(a) }catch(err){ console.log(err) } I'm not sure of the exact reasoning behind it or if that's documented somewhere but I could take a guess: Freezing any assignment to a variable is not easy or even necessarily possible outside of using new-ish or even as of yet unimplemented browser features. Also

Updating large data structures in idiomatic Scala

拥有回忆 提交于 2019-12-05 19:41:22
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 have a reference to some objects that need to be updated, you can't just call setters on them but you need

How do I assign a conditional variable without mutation?

蹲街弑〆低调 提交于 2019-12-05 19:33:14
I am adhering to strict functional programming principles with no mutation. How can I write something like the below code in a way that doesn't mutate the greeting variable, and without returning it within each if block? const greet = (name, time) => { let greeting = 'Morning'; if(time >= 12) { greeting = 'Afternoon'; } if(time >= 17) { greeting = 'Evening'; } return `Good ${greeting} ${name}!`; }; If it was just two conditions I would do the following, but it won't work when there are 3 conditions: const greeting = time > 12 ? 'Afternoon' : 'Morning' Jonas Wilms Ternary expressions can be

Java String Mutability - java.lang.NoSuchFieldException: offset

我的未来我决定 提交于 2019-12-05 19:10:33
I'm new to Java and I saw a Q&A section here with two examples where mutability is removed. Upon testing MutableString.java : import java.lang.reflect.Field; public class MutableString { public static void main(String[] args) { String s = "Immutable"; String t = "Notreally"; mutate(s, t); StdOut.println(t); // strings are interned so this doesn't even print "Immutable" (!) StdOut.println("Immutable"); } // change the first min(|s|, |t|) characters of s to t public static void mutate(String s, String t) { try { Field val = String.class.getDeclaredField("value"); Field off = String.class

Does “<-” mean assigning a variable in Haskell?

假如想象 提交于 2019-12-05 18:57:08
Just started Haskell, it's said that everything in Haskell is "immutable" except IO package. So when I bind a name to something, it's always something immutable? Question, like below: Prelude> let removeLower x=[c|c<-x, c `elem` ['A'..'Z']] Prelude> removeLower "aseruiiUIUIdkf" "UIUI" So here: 1. “removeLower" is an immutable? Even it's a function object? But I can still use "let" to assign something else to this name. 2. inside the function "c<-x" seems that "c" is a variable. It is assigned by list x's values. I'm using the word "variable" from C language, not sure how Haskell name all its