immutability

Tuple declaration in Python

江枫思渺然 提交于 2021-01-27 04:39:41
问题 In python, one can declare a tuple explicitly with parenthesis as such: >>> x = (0.25, 0.25, 0.25, 0.25) >>> x (0.25, 0.25, 0.25, 0.25) >>> type(x) <type 'tuple'> Alternatively, without parenthesis, python automatically packs its into a immutable tuple: >>> x = 0.25, 0.25, 0.25, 0.25 >>> x (0.25, 0.25, 0.25, 0.25) >>> type(x) <type 'tuple'> Is there a pythonic style to declare a tuple? If so, please also reference the relevant PEP or link. There's no difference in the "end-product" of

Convert immutable Bitmap file to mutable Bitmap

拟墨画扇 提交于 2021-01-19 14:19:16
问题 A: Bitmap immutableBmp= BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.sample); mutableBitmap=immutableBmp.copy(Bitmap.Config.ARGB_8888, true); B: Bitmap immutableBmp= BitmapFactory.decodeFile(filePath); mutableBitmap=immutableBmp.copy(Bitmap.Config.ARGB_8888, true); C: BitmapFactory.Options options = new BitmapFactory.Options(); options.inMutable=true; myBitmap=BitmapFactory.decodeFile(filePath,options); A works but B and C don't. I am trying to convert an

Convert immutable Bitmap file to mutable Bitmap

懵懂的女人 提交于 2021-01-19 14:00:30
问题 A: Bitmap immutableBmp= BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.sample); mutableBitmap=immutableBmp.copy(Bitmap.Config.ARGB_8888, true); B: Bitmap immutableBmp= BitmapFactory.decodeFile(filePath); mutableBitmap=immutableBmp.copy(Bitmap.Config.ARGB_8888, true); C: BitmapFactory.Options options = new BitmapFactory.Options(); options.inMutable=true; myBitmap=BitmapFactory.decodeFile(filePath,options); A works but B and C don't. I am trying to convert an

Can I change String object's value passed to my method?

丶灬走出姿态 提交于 2020-12-29 08:55:20
问题 I found the following question Is Java "pass-by-reference" or "pass-by-value"?. I read almost all of it, but could not find out yet what should I do if I want the foo(-) method, to change my String 's value ? (maybe or not reference too, it doesn't matter to me). void foo(String errorText){ errorText="bla bla"; } int main(){ String error="initial"; foo(error); System.out.println(error); } I want to see bla bla on the console. Is it possible? 回答1: You can't change the value of errorText in foo

Can I change String object's value passed to my method?

主宰稳场 提交于 2020-12-29 08:55:01
问题 I found the following question Is Java "pass-by-reference" or "pass-by-value"?. I read almost all of it, but could not find out yet what should I do if I want the foo(-) method, to change my String 's value ? (maybe or not reference too, it doesn't matter to me). void foo(String errorText){ errorText="bla bla"; } int main(){ String error="initial"; foo(error); System.out.println(error); } I want to see bla bla on the console. Is it possible? 回答1: You can't change the value of errorText in foo

Replace element at specific position in an array without mutating it

我的梦境 提交于 2020-12-27 08:12:46
问题 How can the following operation be done without mutating the array: let array = ['item1']; console.log(array); // ['item1'] array[2] = 'item2'; // array is mutated console.log(array); // ['item1', undefined, 'item2'] In the above code, array variable is mutated. How can I perform the same operation without mutating the array? 回答1: You can use Object.assign : Object.assign([], array, {2: newItem}); 回答2: You can simply set up a a new array as such: const newItemArray = array.slice(); And then

Replace element at specific position in an array without mutating it

一个人想着一个人 提交于 2020-12-27 08:09:18
问题 How can the following operation be done without mutating the array: let array = ['item1']; console.log(array); // ['item1'] array[2] = 'item2'; // array is mutated console.log(array); // ['item1', undefined, 'item2'] In the above code, array variable is mutated. How can I perform the same operation without mutating the array? 回答1: You can use Object.assign : Object.assign([], array, {2: newItem}); 回答2: You can simply set up a a new array as such: const newItemArray = array.slice(); And then

@Select of nested object and immutability of state

谁都会走 提交于 2020-12-15 05:41:11
问题 I am using NGXS for a while and found that if you use an object or array in @Select return it can break the immutability of the sate in the component. Example: state: AppStateModel = { justValue: true, complexObject: { a:1, b:2} } and then two selectors: // Here in a component we will get access to object by link and can modify it in state without patchState or setState @Selector() static getComplexObject(state: AppStateModel) { return state.complexObject; } // That will work fine since JS