immutability

Vector or MutableList / ListBuffer for performance

落爺英雄遲暮 提交于 2021-02-07 03:30:36
问题 Apologies if this is a duplicate - I did a few searches and didn't quite find what I need. We have a performance critical piece of our application that converts a Play 2.0 Enumerator (can be thought of as a Stream ) of incoming data to a List (or similar). We will use the fold method on Enumerator and the question is what will be the most performant way to do it. (I will use Stream instead of Enumerator in the code, but the idea should be the same.) val incoming: Stream[Int] = ??? val result:

Vector or MutableList / ListBuffer for performance

回眸只為那壹抹淺笑 提交于 2021-02-07 03:29:05
问题 Apologies if this is a duplicate - I did a few searches and didn't quite find what I need. We have a performance critical piece of our application that converts a Play 2.0 Enumerator (can be thought of as a Stream ) of incoming data to a List (or similar). We will use the fold method on Enumerator and the question is what will be the most performant way to do it. (I will use Stream instead of Enumerator in the code, but the idea should be the same.) val incoming: Stream[Int] = ??? val result:

PHP pass objects by value

梦想的初衷 提交于 2021-02-05 09:27:55
问题 I'd like to implement a pure function in PHP How do I pass an object by value and not by reference? In other words, this is the expected output: function change($obj) { $obj->set_value(2); } $obj = new Object(); $obj->set_value(1); change($obj); echo $obj->get_value(); // 1 回答1: read here: http://php.net/manual/en/language.oop5.cloning.php you really shouldn't pass by value, as that would require a deep copy aka. deep clone, or an insane amount allocated for for parameters.. if you really

Understanding Mutability and Multiple Variable Assignment to Class Objects in Python

五迷三道 提交于 2021-01-29 06:49:35
问题 I'm looking for some clarification regarding mutability and class objects. From what I understand, variables in Python are about assigning a variable name to an object. If that object is immutable then when we set two variables to the same object, it'll be two separate copies (e.g. a = b = 3 so a changing to 4 will not affect b because 3 is a number, an example of an immutable object). However, if an object is mutable, then changing the value in one variable assignment will naturally change

How can I return immutable data from redux reducer?

时光总嘲笑我的痴心妄想 提交于 2021-01-29 06:12:56
问题 For a learning and test project, I'm trying to return immutable redux data from reducer because safe data for component. this is my reducer code : function itemsReducer(state = [], action) { switch(action.type) { case 'ITEMS': return [...action.data] default: return state } } And this is my loop code : <ul> { this.props.items.map((item) => ( <li>{item.name.first} {item.name.last}</li> )) } </ul> now every things work right, but after change on props with this method : change() { this.props

How to use ImmutableJS Map with TypeScript

爱⌒轻易说出口 提交于 2021-01-29 02:53:49
问题 I have a tree structure that looks something like this: interface TreeData { id : number; text : string; children : TreeData[]; } I want to wrap this up into an Immutable Map but since I'm using TypeScript, I would like some type safety around using get and set. I found 2 articles online for suggestions on how one might be able to do this, see: https://github.com/facebook/immutable-js/issues/683 https://blog.mayflower.de/6630-typescript-redux-immutablejs.html However my problem is that my

How does let/var resolve mutability? [duplicate]

末鹿安然 提交于 2021-01-28 02:50:29
问题 This question already has answers here : Immutable/Mutable Collections in Swift (7 answers) Closed 3 years ago . I don't have any problem, i would just like some clarification on an issue regarding mutability. In Objective-C we would use for example NSMutableArray to get a mutable array and an NSArray to get an immutable one. I don't know much about the inner workings of the two, but from what I can remember I believe that the difference is that NSArray only reserves an amount of memory

Is marking String type reference as Volatile safe?

半城伤御伤魂 提交于 2021-01-27 08:00:59
问题 I've read some posts and articles saying that we shouldn't declare java objects as volatile, because as a result, only the reference becomes volatile. Here are some examples: link-1 link-2 link-3 What Sonar suggests is 'Non-primitive fields should not be "volatile"', however, it also suggests that the problem described refers to mutable objects 'Similarly, marking a mutable object field volatile means the object reference is volatile but the object itself is not'. My question is: is it safe

Is marking String type reference as Volatile safe?

怎甘沉沦 提交于 2021-01-27 07:56:53
问题 I've read some posts and articles saying that we shouldn't declare java objects as volatile, because as a result, only the reference becomes volatile. Here are some examples: link-1 link-2 link-3 What Sonar suggests is 'Non-primitive fields should not be "volatile"', however, it also suggests that the problem described refers to mutable objects 'Similarly, marking a mutable object field volatile means the object reference is volatile but the object itself is not'. My question is: is it safe

Is it possible to write an immutable doubly linked list?

元气小坏坏 提交于 2021-01-27 06:16:34
问题 I feel a little stupid for asking this, but I'm currently learning functional programming and completed an exercise on creating singly linked lists and it just got me thinking, is it even possible to create an immutable doubly linked list? Suppose the list A :: B, at time of construction, A needs to know about B, but B also needs to know about A. I've been doing this in Scala, so I'm not sure if it's specific to Scala, but I can't picture how that would work. I'm not looking for a substitute