immutability

python update outer passed value from within a for loop

时间秒杀一切 提交于 2019-12-10 11:45:04
问题 My question is a slight variation of How do I pass a variable by reference? - however I still can't find a simple solution to the problem illustrated below: how can I alter the values of the outer string variables from within a for-loop? str1 = 'before' str2 = 'before' for i in str1,str2: print "%s" % (i) i = "after" for i in str1,str2: print "%s" % (i) Thanks for the helpful responses below however they haven't really helped with my specific problem, the code below is less generic but better

Using F# Datatypes in C#

被刻印的时光 ゝ 提交于 2019-12-10 10:53:27
问题 More particularly, I really want an immutable/shared linked list, and I think having immutable maps and sets would be nice too. As long as I don't have to worry about the core implementation, I can easily add extension methods/subclass/wrap it to provide a reasonably slick external interface for myself to use. Is there any reason I shouldn't do this? Performance, incompatibility, etc.? 回答1: The types in the F# library (such as Set , Map and list ) were not designed to be used from C#, so I

Python: can I modify a Tuple?

99封情书 提交于 2019-12-10 08:43:46
问题 I have a 2 D tuple (Actually I thought, it was a list.. but the error says its a tuple) But anyways.. The tuple is of form: (floatnumber_val, prod_id) now I have a dictionary which contains key-> prod_id and value prod_name now.. i want to change the prod_id in tuple to prod_name So this is waht I did #if prodName is the tuple # prodDict is the dictionary for i in range(len(prodName)): key = prodName[i][1] # get the prodid if prodDict.has_key(key): value = prodDict[key] prodName[i][1] = value

How do I assign a conditional variable without mutation?

こ雲淡風輕ζ 提交于 2019-12-10 07:14:48
问题 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

safe publication of mutable object

一个人想着一个人 提交于 2019-12-10 04:35:03
问题 I read several related questions, but none of them explains ways of safe publication of the Holder. I am still confused about example from Java Concurrency in Practice, section 3.5: There is the class Holder: public Holder { private int n; public Holder(int n) { this.n = n }; public void assertSanity() { if(n != n) throw new AssertionError("This statement is false."); } } and its unsafe publication: //unsafe publication public Holder holder; public void initialize() { holder = new Holder(42);

Mapping an object to an immutable object with builder (using immutables annotation processor) in mapstruct

别说谁变了你拦得住时间么 提交于 2019-12-10 03:36:28
问题 We are using the immutables framework to generate all DTOs. Now we would like to map these objects one to another with mapstruct. But the generated DTOs are immutable and have no setters and no constructor, corresponding to the builder pattern. They are only filled through the corresponding builder accessed by a static builder() -method. We instead tried to map DTO1 to DTO2.Builder which would work if mapstruct would recognize the setter in the Builder but these do not have void return type

Understanding immutable composite types with fields of mutable types in Julia

こ雲淡風輕ζ 提交于 2019-12-10 03:24:23
问题 Initial note: I'm working in Julia, but this question probably applies to many languages. Setup: I have a composite type as follows: type MyType x::Vector{String} end I write some methods to act on MyType . For example, I write a method that allows me to insert a new element in x , e.g. function insert!(d::MyType, itemToInsert::String) . Question: Should MyType be mutable or immutable? My understanding: I've read the Julia docs on this, as well as more general (and highly upvoted) questions

What is the cleanest way to remove an element from an immutable array in JS? [duplicate]

痴心易碎 提交于 2019-12-10 02:52:02
问题 This question already has answers here : Is this the correct way to delete an item using redux? (4 answers) Closed 2 years ago . I need to remove an element from an array that is a state of a React Component. Which means that it is an immutable object. Adding a element is easy using spread syntax. return { ...state, locations: [...state.locations, {}] }; Removing is a little more tricky. I need to use an intermediate object. var l = [...state.locations] l.splice(index, 1) return { ...state,

Do not declare read only mutable reference types - why not?

柔情痞子 提交于 2019-12-10 01:16:39
问题 I have been reading this question and a few other answers and whilst I get the difference between changing the reference and changing the state of the current instance I'm not certain why this means that I shouldn't mark it readonly. Is this because marking something as readonly tells the compiler something special about the instance and so it is able to then treat it as thread safe when it actually might not be? Presumably there are situations where I don't want the instance to be able to be

Scala: var List vs val MutableList

不想你离开。 提交于 2019-12-10 01:14:18
问题 In Odersky et al's Scala book, they say use lists. I haven't read the book cover to cover but all the examples seem to use val List. As I understand it one also is encouraged to use vals over vars. But in most applications is there not a trade off between using a var List or a val MutableList?. Obviously we use a val List when we can. But is it good practice to be using a lot of var Lists (or var Vectors etc)? I'm pretty new to Scala coming from C#. There I had a lot of: public List<T> myList