mutable

How are mutable arrays implemented in Haskell?

旧街凉风 提交于 2019-11-27 04:21:57
问题 I've read many research papers on this topic, and they usually argue that arrays are implemented using Monads. But none of these papers gave a clear definition of how the "type" Array itself should be defined, they only gave definitions for the functions using monads to access or modify this type. How are arrays, having O(1) time to access or modify an indexed element, implemented in Haskell ?! (such as STUArray and MArray) 回答1: How are arrays, having O(1) time to access or modify an indexed

Why are Java wrapper classes immutable?

拜拜、爱过 提交于 2019-11-27 04:20:58
问题 I know the usual reasons that apply to general immutable classes, viz can not change as a side effect easy to reason about their state inherently thread safe no need to provide clone/copy constructor/factory copy method instance caching no need for defensive copies. However, wrapper classes represent primitive types, and primitive types are mutable. So why aren't wrapper classes mutable? 回答1: However, wrapper classes represent primitive types, and primitive types (except String) are mutable.

F#: let mutable vs. ref

江枫思渺然 提交于 2019-11-27 03:02:54
First, I acknowledge the possibility that this question could be a duplicate; just let me know. I'm curious what the general "best practice" is for those situations when mutability is desired. F# seems to offer two facilities for this: the let mutable binding, which seems to work like variables in "most" languages, and the reference cell (created with the ref function) that requires explicit dereferencing to use. There are a couple of cases where one is "forced" into one or the other: .NET interop tends to use mutable with <- , and in workflow computations one must use ref with := . So those

Are strings in Ruby mutable? [duplicate]

只谈情不闲聊 提交于 2019-11-27 01:15:13
问题 This question already has an answer here: Are strings mutable in Ruby? 2 answers Consider the following code: $ irb > s = "asd" > s.object_id # prints 2171223360 > s[0] = ?z # s is now "zsd" > s.object_id # prints 2171223360 (same as before) > s += "hello" # s is now "zsdhello" > s.object_id # prints 2171224560 (now it's different) Seems like individual characters can be changed w/o creating a new string. However appending to the string apparently creates a new string. Are strings in Ruby

Loading a resource to a mutable bitmap

允我心安 提交于 2019-11-26 22:53:36
问题 I am loading a bitmap from a resource like so: Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image); What I want to do is make some changes to the bitmap before It gets drawn to the main canvas in my draw method (As it would seem wasteful to repeat lots of drawing in my main loop when it isn't going to change). I am making the changes to the bitmap with the following: Canvas c = new Canvas(mBackground); c.drawARGB(...); // etc So naturally I get an exception java.lang

Why did Matz choose to make Strings mutable by default in Ruby?

别来无恙 提交于 2019-11-26 22:47:17
问题 It's the reverse of this question: Why can't strings be mutable in Java and .NET? Was this choice made in Ruby only because operations (appends and such) are efficient on mutable strings, or was there some other reason? (If it's only efficiency, that would seem peculiar, since the design of Ruby seems otherwise to not put a high premium on faciliating efficient implementation.) 回答1: This is in line with Ruby's design, as you note. Immutable strings are more efficient than mutable strings -

Logical const in D

南笙酒味 提交于 2019-11-26 21:47:22
问题 D has two types of constness: immutable variables are ones that were declared immutable, and always will be immutable, while const variables are simply read only versions of an object. Logical const is when a function is marked as const , but allows write access to one or more member variables. The typical use of this is for lazy evaluation, e.g. (in C++) struct Matrix { double determinant() const { if ( m_dirty ) { m_determinant = /* expensive calculation */; m_dirty = false; } return m

Why does using `arg=None` fix Python's mutable default argument issue?

南笙酒味 提交于 2019-11-26 19:01:09
I'm at the point in learning Python where I'm dealing with the Mutable Default Argument problem . # BAD: if `a_list` is not passed in, the default will wrongly retain its contents between successive function calls def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list # GOOD: if `a_list` is not passed in, the default will always correctly be [] def good_append(new_item, a_list=None): if a_list is None: a_list = [] a_list.append(new_item) return a_list I understand that a_list is initialized only when the def statement is first encountered, and that's why subsequent calls of

a mutable type inside an immutable container

牧云@^-^@ 提交于 2019-11-26 17:55:55
I'm a bit confused about modifying tuple members. The following doesn't work: >>> thing = (['a'],) >>> thing[0] = ['b'] TypeError: 'tuple' object does not support item assignment >>> thing (['a'],) But this does work: >>> thing[0][0] = 'b' >>> thing (['b'],) Also works: >>> thing[0].append('c') >>> thing (['b', 'c'],) Doesn't work, and works (huh?!): >>> thing[0] += 'd' TypeError: 'tuple' object does not support item assignment >>> thing (['b', 'c', 'd'],) Seemingly equivalent to previous, but works: >>> e = thing[0] >>> e += 'e' >>> thing (['b', 'c', 'd', 'e'],) So what exactly are the rules

Is Integer Immutable

耗尽温柔 提交于 2019-11-26 17:15:25
I know this is probably very stupid, but a lot of places claim that the Integer class in Java is immutable, yet the following code: Integer a=3; Integer b=3; a+=b; System.out.println(a); Executes without any trouble giving the (expected) result 6. So effectively the value of a has changed. Doesn't that mean Integer is mutable? Secondary question and a little offtopic: "Immutable classes do not need copy constructors". Anyone care to explain why? Travis Webb Immutable does not mean that a can never equal another value. For example, String is immutable too, but I can still do this: String str =