immutability

using immutability-helper to $merge an array

懵懂的女人 提交于 2019-12-11 05:37:13
问题 I get the following error from the code below: Error: update(): $merge expects a target of type 'object'; got undefined I am trying to merge 2 arrays while only updating certain values in the items in the original array. For example, I want to update the status and updated properties but not the members array property. I think that the issue may be with the use of index in the update method but that is just a guess. Any ideas? If there is a better approach, I would like to know it. const

Change the value of a property of a struct in C# [duplicate]

你说的曾经没有我的故事 提交于 2019-12-11 05:29:10
问题 This question already has answers here : Why are C# structs immutable? (5 answers) Closed 5 years ago . I was reading a book and found out that structs are actually immutable objects. But they have getters and setters. I was wondering if a property of structs can be changed after it has been created. public struct Test { public string str {get; set; } public int int1 {get; set; } } Can the values of 'str' and 'int1' be changed once they have been assigned a value? 回答1: Structs can be either

Scala: How to avoid var here

流过昼夜 提交于 2019-12-11 05:26:02
问题 I have a code snippet like this: def until(terminationCond: List[A]=>Boolean, combiner: List[A]=>List[A] )(obj: List[A]): A = { var tempObj = obj while(!terminationCond(tempObj)) { tempObj = combiner(obj) } tempObj.head } I am looking out a way to write this code from functional programming style, avoiding any mutable types. 回答1: Using recursion: @tailrec def doUntilTerm(obj: List[A]) = if (terminationCond(obj)) obj.head else doUntilTerm(obj) 回答2: I find the following a little more

React redux - updating nested array in state

淺唱寂寞╮ 提交于 2019-12-11 05:14:07
问题 I'm trying some app in react redux and i have a problem with updating (push, remove, update) the nested array in state. I have some object called service like this: { name: 'xzy', properties: [ { id: 1, sName: 'xxx'}, { id: 2, sName: 'zzz'}, ] } Whatever I did (in case of adding property to collection) in the reducer with the properties collection generate problem that all properties got same values as the last I had recently added -> Added property object is in service properties collection

Are immutable objects always threadsafe?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 04:18:32
问题 It is safe to assume that working with or passing around an immutable object would always be threadsafe? 回答1: Yes. If an object is truly immutable, with no internal mutations occurring, then the object itself will be threadsafe. (Whether you handle the object and pass it around in a threadsafe manner is another matter!) What do I mean by "internal mutations"? Many objects appear to be immutable from the outside - for example, no property setters or other members that would obviously trigger

Wrapping big list in Python 2.7 to make it immutable

家住魔仙堡 提交于 2019-12-11 03:19:31
问题 In case I have a really big list (>100k elements) that can be retrieved from some object through function call, is there a way to wrap that list to make it immutable to the caller without copying it to tuple ? In the following example I have only one list field, but the solution should work for any number of list fields. class NumieHolder(object): def __init__(self): self._numies = [] def add(self, new_numie): self._numies.append(new_numie) @property def numies(self): # return numies embedded

Returning a mutable reference that is behind an immutable reference, passed to the function

时间秒杀一切 提交于 2019-12-11 03:04:29
问题 How is returning a mutable reference that is behind an immutable reference, passed as an argument to the function, handled? struct Foo { i: i32 } struct Bar<'b> { f: &'b mut Foo } impl<'a: 'b, 'b> Bar<'b> { fn func(&'a self) -> &'b mut Foo { self.f } } fn main() { let mut foo = Foo { i: 1 }; let bar = Bar { f: &mut foo }; bar.func(); } gives the following error: error[E0389]: cannot borrow data mutably in a `&` reference --> src/main.rs:9:14 | 8 | fn func(&'a self) -> &'b mut Foo { | --------

How do I create and initialize an immutable array?

六月ゝ 毕业季﹏ 提交于 2019-12-11 02:20:08
问题 I want to create an array. I don't need the array to be mutable, and at the time of creation, I have all the information I need to calculate the i-th member of the array. However, can't figure out how to create an immutable array in Rust. Here's what I have now: let mut my_array: [f32; 4] = [0.0; 4]; for i in 0..4 { // some calculation, doesn't matter what exactly my_array[i] = some_function(i); } And here's what I want: let my_array: [f32; 4] = array_factory!(4, some_function); How can I

“Cannot assign to property: ”any“ is immutable” error

给你一囗甜甜゛ 提交于 2019-12-11 01:45:04
问题 tiledViewsStack contains UIImageViews. I'm trying to give each UIImageView in the array a new center coordinate. Not sure why I'm getting this error for the last line in the for loop... var tiledViewsStack = [AnyObject]() var randLocInt = Int() var randLoc = CGPoint() for var any in tiledViewsStack { randLocInt = Int((arc4random()*10) % 9) // 0, --- 8 randLoc = allCenters[randLocInt].CGPointValue() any.center = randLoc } 回答1: Needed to make tiledViewsStack a stack of UIImages, not AnyObjects.

Error: “Cannot assign to immutable expression of type 'Bool'”?

本秂侑毒 提交于 2019-12-11 01:35:45
问题 How do I fix this? I'm a new coder. Thank you I get the follow error: "Cannot assign to immutable expression of type 'Bool'" When I try to set the "isSelected" to false and true @IBAction func onFilter(_ sender: Any) { if ((sender as AnyObject).isSelected == true) { hideSecondaryMenu() (sender as AnyObject).isSelected = false } else { showSecondaryMenu() (sender as AnyObject).isSelected = true } } 回答1: You are getting this error because when you are converting sender to AnyObject you are