mutable

Hashtable of mutable variable in Ocaml

℡╲_俬逩灬. 提交于 2019-12-04 07:22:11
I need to use hashtable of mutable variable in Ocaml, but it doesn't work out. let link = Hashtbl.create 3;; let a = ref [1;2];; let b = ref [3;4];; Hashtbl.add link a b;; # Hashtbl.mem link a;; - : bool = true # a := 5::!a;; - : unit = () # Hashtbl.mem link a;; - : bool = false Is there any way to make it works? Mutable variables that may happen to have the same content can still be distinguished because they are stored at different locations in memory. They can be compared with the physical equality operator ( == ). However, OCaml doesn't provide anything better than equality, it doesn't

Update mutable HashMap value which is a mutable collection

妖精的绣舞 提交于 2019-12-04 04:20:06
问题 I'm have a map that looks like this: Map[ A -> Collection[B]] . This map gets updated in a loop - the special thing is however, that updates mostly just mean adding an element B to the Collection[B] (for some key A). I am trying to find out if I can get some speedup by changing the type of my Collection from List[ ] to ListBuffer[ ]. Up to now my code looked like this (simplified): var incoming = new HashMap[A, List[B]() { override def default(a: A) = List() } .. for(b < someCollectionOfBs){

How do you return multiple values and assign them to mutable variables?

穿精又带淫゛_ 提交于 2019-12-04 02:52:26
This is what I have so far. let Swap (left : int , right : int ) = (right, left) let mutable x = 5 let mutable y = 10 let (newX, newY) = Swap(x, y) //<--this works //none of these seem to work //x, y <- Swap(x, y) //(x, y) <- Swap(x, y) //(x, y) <- Swap(x, y) //do (x, y) = Swap(x, y) //let (x, y) = Swap(x, y) //do (x, y) <- Swap(x, y) //let (x, y) <- Swap(x, y) You can't; there's no syntax to update 'more than one mutable variable' with a single assignment. Of course you can do let newX, newY = Swap(x,y) x <- newX y <- newY The code you have commented doesn't work because when you write "x, y"

Scala: Contains in mutable and immutable sets

让人想犯罪 __ 提交于 2019-12-04 02:02:06
I've discovered a strange behavior for mutable sets which I cannot understand: I have a object which I want to add to a set. The equals method for the class is overridden. When I add two different objects to the set, which produces the same output for equals method, I get a different behavior between mutable and immutable sets for the contains method. Here is the code snippet: class Test(text:String){ override def equals(obj:Any) = obj match { case t: Test => if (t.text == this.text) true else false case _ => false } override def toString = text } val mutableSet:scala.collection.mutable.Set

How do you declare the values of a dictionary entry as mutable?

自古美人都是妖i 提交于 2019-12-04 00:46:12
The Google yields plenty of example of adding and deleting entries in an F# dictionary (or other collection). But I don't see examples to the equivalent of myDict["Key"] = MyValue; I've tried myDict.["Key"] <- MyValue I have also attempted to declare the Dictionary as Dictionary<string, mutable string> as well several variants on this. However, I haven't hit on the correct combination yet... if it is actually possible in F#. Edit: The offending code is: type Config(?fileName : string) = let fileName = defaultArg fileName @"C:\path\myConfigs.ini" static let settings = dict[ "Setting1", "1";

Java 可变对象和不可变对象

为君一笑 提交于 2019-12-03 20:53:26
一、简单定义 不可变对象(Immutable Objects)即对象一旦被创建它的状态(对象的数据,也即对象属性值)就不能改变 ,反之即为可变对象(Mutable Objects)。 不可变对象的类即为不可变类(Immutable Class)。Java平台类库中包含许多不可变类,如String、基本类型的包装类、BigInteger 和 BigDecimal等。 二、优缺点 不可变对象有很多优点: 构造、测试和使用都很简单 线程安全且没有同步问题,不需要担心数据会被其它线程修改 当用作类的属性时不需要保护性拷贝 可以很好的用作Map键值和Set元素 不可变对象最大的缺点就是创建对象的开销,因为每一步操作都会产生一个新的对象。 三、编写不可变类 可以遵照以下几点来编写 一个不可变类 : 确保类不能被继承 - 将类声明为final, 或者使用静态工厂并声明构造器为private 声明属性为private 和 final 不要提供任何可以修改对象状态的方法 - 不仅仅是set方法, 还有任何其它可以改变状态的方法 如果类有任何可变对象属性, 那么当它们在类和类的调用者间传递的时候必须被保护性拷贝 代码-1: import java.util.Date; /** * Planet是一个不可变类,因为当它构造完成之后没有办法改变它的状态 */ public final class

Why is a string key for a hash frozen?

感情迁移 提交于 2019-12-03 16:07:29
问题 According to the specification, strings that are used as a key to a hash are duplicated and frozen. Other mutable objects do not seem to have such special consideration. For example, with an array key, the following is possible. a = [0] h = {a => :a} h.keys.first[0] = 1 h # => {[1] => :a} h[[1]] # => nil h.rehash h[[1]] # => :a On the other hand, a similar thing cannot be done with a string key. s = "a" h = {s => :s} h.keys.first.upcase! # => RuntimeError: can't modify frozen String Why is

Why is MutableString deprecated in Python?

断了今生、忘了曾经 提交于 2019-12-03 15:16:37
问题 Why was the MutableString class deprecated in Python 2.6; and why was it removed in Python 3? 回答1: The MutableString class was meant to be educational, and not to be used in real programs. If you look at the implementation, you'd see that you can't really use this in a serious application requiring mutable strings. If you need mutable bytestrings, you might consider using bytearray that's available in Python 2.6 and 3.x. The implementation doesn't create new strings every time you modify the

Haskell Data Type With References

谁说我不能喝 提交于 2019-12-03 13:56:19
I'm implementing Ukkonen's algorithm, which requires that all leaves of a tree contain a reference to the same integer, and I'm doing it in Haskell to learn more about the language. However, I'm having a hard time writing out a data type that does this. -- Node has children, indexes of info on the edge -- to it, and an optional suffix link. -- Leaf has a beginning index of the info, but the -- end index is always an incrementing variable index. data STree = Node [STree] (Int, Int) (Maybe STree) | Leaf (Int, ??? ) How can I put the reference in the Leaf type declaration? 来源: https:/

const_cast VS mutable ? any difference?

我是研究僧i 提交于 2019-12-03 12:59:36
问题 From my understanding , mutable cancels the constness of a variable Class A { void foo() const { m_a = 5; } mutable int m_a; }; But also const_cast : void print (char * str) { cout << str << endl; } int main () { const char * c = "this is a line"; print ( const_cast<char *> (c) ); return 0; } So , what changes one from the other ? Thanks 回答1: const_cast cannot cancel constness of an object. const_cast can only remove constness from an access path to an object. Access path is a pointer or a