immutability

Mutability of the **kwargs argument in Python

别说谁变了你拦得住时间么 提交于 2019-11-27 07:41:58
问题 Consider a case where I change the kwargs dict inside a method: def print_arg(**kwargs): print kwargs.pop('key') If I call the method pop_arg with a dictionary like this: mydict = {'key':'value'} print_arg(**mydict) will mydict be changed by this call? I am also interested in a more detailed explanation of the underlying method calling mechanism that lets mydict change or not. 回答1: Let's see: import dis dis.dis(lambda: print_arg(**{'key': 'value'})) 6 0 LOAD_GLOBAL 0 (print_arg) 3 BUILD_MAP 1

Remove element from nested redux state

落花浮王杯 提交于 2019-11-27 07:37:41
问题 I want to remove a element from my redux state. Im using seamless-immutable and its API to operate on the state. But I couldn't find any nice looking way to delete a element when the state is nested. So the element I want to delete is: state.shoppingcartReducer.products[articleNr] Thanks! import Immutable from 'seamless-immutable' import { addToShoppingcart, createShoppingCart } from '../actions/shoppingcartActions' const CREATE_SHOPPING_CART = 'CREATE_SHOPPING_CART' const ADD_PRODUCT = 'ADD

Kotlin Instantiate Immutable List

[亡魂溺海] 提交于 2019-11-27 07:08:36
问题 I've started using Kotlin as a substitute for java and quite like it. However, I've been unable to find a solution to this without jumping back into java-land: I have an Iterable<SomeObject> and need to convert it to a list so I can iterate through it more than once. This is an obvious application of an immutable list, as all I need to do is read it several times. How do I actually put that data in the list at the beginning though? (I know it's an interface, but I've been unable to find an

Immutable/Mutable Collections in Swift

天涯浪子 提交于 2019-11-27 06:21:54
I was referring to Apple's Swift programming guide for understanding creation of Mutable/ immutable objects(Array, Dictionary, Sets, Data) in Swift language. But I could't understand how to create a immutable collections in Swift. I would like to see the equivalents in Swift for the following in Objective-C Immutable Array NSArray *imArray = [[NSArray alloc]initWithObjects:@"First",@"Second",@"Third",nil]; Mutable Array NSMutableArray *mArray = [[NSMutableArray alloc]initWithObjects:@"First",@"Second",@"Third",nil]; [mArray addObject:@"Fourth"]; Immutable Dictionary NSDictionary *imDictionary

C# and immutability and readonly fields… a lie?

一世执手 提交于 2019-11-27 05:35:29
问题 I have found that People claim that using all readonly fields in a class does not necessarily make that class's instance immutable because there are "ways" to change the readonly field values even after initialization (construction). How? What ways? So my question is when can we really have a "real" immutable object in C#, that I can safely use in threading? Also do anonymous types create immutable objects? And some say LINQ uses immutable objecst internally. How exactly? 回答1: You've asked

Why final keyword is necessary for immutable class?

久未见 提交于 2019-11-27 05:22:41
问题 Could you please clarify that why final keyword is required before class when we are making it an immutable one. I mean, if we declare all of it's attributes as private and final, then also it is an immutable class, isn't it? Sorry if the question seems easy, but i am truly confused about it. Help me out. Editted: I know that a class declared final can't be subclassed.. But if each attribute is private and final then what difference does that make? 回答1: As stacker says, final makes sure the

Does (or will) C# include features for side-effects verification?

若如初见. 提交于 2019-11-27 05:20:50
问题 I know C# is getting a lot of parallel programming support, but AFAIK there is still no constructs for side-effects verification, right? I assume it's more tricky now that C# is already laid out. But are there plans to get this in? Or is F# the only .NET language that has constructs for side-effects verification? 回答1: C# the language isn't, but .NET the framework may be. The Contracts library + the static analysis tools being introduced in .NET 4 might introduce these: Microsoft is using

Swift make method parameter mutable?

陌路散爱 提交于 2019-11-27 05:20:43
问题 How can I deal with this error without creating additional variable? func reduceToZero(x:Int) -> Int { while (x != 0) { x = x-1 // ERROR: cannot assign to 'let' value 'x' } return x } I don't want to create additional variable just to store the value of x. Is it even possible to do what I want? 回答1: As stated in other answers, as of Swift 3 placing var before a variable has been deprecated. Though not stated in other answers is the ability to declare an inout parameter. Think: passing in a

cannot borrow `self.x` as immutable because `*self` is also borrowed as mutable

徘徊边缘 提交于 2019-11-27 05:16:13
First, let the code speak: #[derive(Debug)] struct Bar; #[derive(Debug)] struct Qux { baz: bool } #[derive(Debug)] struct Foo { bars: Vec<Bar>, qux: Qux, } impl Foo { fn get_qux(&mut self) -> &mut Qux { &mut self.qux } fn run(&mut self) { // 1. Fails: let mut qux = self.get_qux(); // 2. Works: // let mut qux = &mut Qux { baz: false }; // 3. Works: // let mut qux = &mut self.qux; let qux_mut = &mut qux; qux_mut.baz = true; for bar in &self.bars { println!("{:?}", bar); } } } fn main() { println!("Hello, world!"); let mut foo = Foo { bars: vec!(), qux: Qux { baz: false } }; foo.run(); } This

Cannot borrow `*x` as mutable because it is also borrowed as immutable

倖福魔咒の 提交于 2019-11-27 04:53:35
问题 I'm making a Combinatory Optimization project to learn Rust and I've got a problem I cannot resolve myself... I've got 2 functions : pub fn get_pareto_front_offline<'a>(scheduling_jobs: &'a Vec<Vec<u32>>, costs_vector: &'a Vec<(u32, u32)>) -> Vec<(&'a Vec<u32>, &'a (u32, u32))> { // ... } and pub fn pareto_approach_offline<'a>(list_of_jobs: &'a mut Vec<Vec<u32>>, neighborhood: &'a mut Vec<Vec<u32>>, costs: &'a Vec<(u32, u32)>) -> Vec<(&'a Vec<u32>, &'a (u32, u32))> { let pareto_front = get