immutability

Closing over immutable variable and accumulate values across multiple iterations as a lambda expression - Java 8

丶灬走出姿态 提交于 2019-12-02 04:16:28
WebTarget in Jersey client is implemented as a immutable object, and any operations to change the state returns a new WebTarget. To add query params to it, which is coming in as a Map<> the following code was written. public WebTarget webTarget(String path, Map<String, String> queryMap) { WebTarget webTarget = client.target(this.address.getUrl()).path(path); if (queryMap != null) queryMap.entrySet().forEach(e -> webTarget.queryParam(e.getKey(), e.getValue())); return webTarget; } The problems here is every call to .queryParam() returns a new WebTarget and I'm stuck at how to accumulate as the

Does the C++ memory model provide guarantees about the operations of constructors [closed]

拥有回忆 提交于 2019-12-02 03:56:28
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . How do you ensure newly constructed immutable objects are safely shareable amongst threads in C++? Does the C++ memory model provide guarantees about the operations of constructors? When you have multiple threads that share access to an object, and the object is modified, race

Implement Kahn's topological sorting algorithm using Python

纵饮孤独 提交于 2019-12-02 03:51:48
Kahn proposed an algorithm in 62 to topologically sort any DAG (directed acyclic graph), pseudo code copied from Wikipedia: L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S add n to tail of L for each node m with an edge e from n to m do remove edge e from the graph # This is a DESTRUCTIVE step! if m has no other incoming edges then insert m into S if graph has edges then return error (graph has at least one cycle) else return L (a topologically sorted order) I need to implement it using IPython3,

How to concat arrays immutable way JS

浪子不回头ぞ 提交于 2019-12-02 03:02:54
问题 I wonder how to contact array that is immutable. Lets imagine I start with array list = [4,1] , and then I receive array from action response like so items = [5,2,6] . How do I concant arrays that the result is [4,1,5,2,6] and that operation is not mutable. Bonus : How do I overwrite items with same id (immutable way)? Lets imagine this our array in store books=[{'id':1, 'title': 'Cool story'}, {'id':2, 'title': 'Bad story'}] . Other array that needs to overwrite books (last sync from API)

Why do immutable classes provide mutators?

拟墨画扇 提交于 2019-12-02 00:50:08
问题 Consider the following code: bdval = new BigDecimal(strval, new MathContext(attrib.getPrecision())); bdval.setScale(attrib.getScale(), RoundingMode.HALF_UP); PMD quite correctly says: Useless operation on Immutable So why do Immutable classes like BigDecimal export mutators for properties? 回答1: setScale() doesn't mutate the BigDecimal it's called on. It returns a copy of the BigDecimal with the new scale value. PMD reports an error because YOUR code is wrong: it ignores the result of the

How to concat arrays immutable way JS

心不动则不痛 提交于 2019-12-01 23:39:59
I wonder how to contact array that is immutable. Lets imagine I start with array list = [4,1] , and then I receive array from action response like so items = [5,2,6] . How do I concant arrays that the result is [4,1,5,2,6] and that operation is not mutable. Bonus : How do I overwrite items with same id (immutable way)? Lets imagine this our array in store books=[{'id':1, 'title': 'Cool story'}, {'id':2, 'title': 'Bad story'}] . Other array that needs to overwrite books (last sync from API) otherArray = [{'id':3, 'title': 'Super story'}, {'id':1, 'title': 'Very cool story'}] . So result should

Why do immutable classes provide mutators?

血红的双手。 提交于 2019-12-01 22:27:43
Consider the following code: bdval = new BigDecimal(strval, new MathContext(attrib.getPrecision())); bdval.setScale(attrib.getScale(), RoundingMode.HALF_UP); PMD quite correctly says: Useless operation on Immutable So why do Immutable classes like BigDecimal export mutators for properties? setScale() doesn't mutate the BigDecimal it's called on. It returns a copy of the BigDecimal with the new scale value. PMD reports an error because YOUR code is wrong: it ignores the result of the operation making the operation useless. Your code should be: bdval = bdval.setScale(attrib.getScale(),

Encapsulation for mutable objects in Java

好久不见. 提交于 2019-12-01 21:59:00
问题 I was studying the "Java SE 7 Programmer I & II Study Guide" and I did not understand the explanation below. class Fortress{ private String name; private ArrayList<Integer> list; Fortress() {list=new ArrayList<Integer>; String getName{return name;} void addToList(int x){list.add(x);} ArrayList getList(){return list;} // line 1 } Which lines of code break encapsulation? Answer: line 9. "When encapsulating a mutable object like an ArrayList, your getter must return a reference to a copy of the

Update nested data arrays of object (Redux)

孤者浪人 提交于 2019-12-01 21:44:59
问题 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 }, ], }], // ... }; 回答1: This should do the trick,

Arrays implementation in erlang

半城伤御伤魂 提交于 2019-12-01 21:10:24
My question is, how are arrays implemented in Erlang, as opposed to lists. With immutable types doing things like, move ([X | Xs], Ys) -> [X | Ys]. Ls = move([1,2,3], [2,3,4]) would take up constant mem in the heap, since this is all reference work. But for the same stuff in arrays move (A1, A2) -> array:set(0, array:get(0,A1),A2). A1 = array:from_list([1,2,3]). A2 = array:from_list([0,2,3,4]). A3 = move(A1,A2). Will move here use size proportional to A2 or will it be able to do this in constant space like with the arrays? To (hopefully) clear things up a little. Remember that in Erlang ALL