immutability

F#: only do one action once for the first event, without mutability/locking?

大憨熊 提交于 2019-12-10 15:45:00
问题 I have this code that downloads a file and tells me in the console how big is the file: use webClient = new WebClient() let lockObj = new Object() let mutable firstProgressEvent = true let onProgress (progressEventArgs: DownloadProgressChangedEventArgs) = lock lockObj (fun _-> if (firstProgressEvent) then let totalSizeInMB = progressEventArgs.TotalBytesToReceive / 1000000L Console.WriteLine ("Starting download of {0}MB...", totalSizeInMB) firstProgressEvent <- false ) webClient

Can a whole program be immutable? [closed]

丶灬走出姿态 提交于 2019-12-10 15:36:49
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . I am familiar with immutability and can design immutable classes, but I have mostly academic knowledge and lacking hands on experience Please refer to the linked image above (not allowed to embed yet) Looking at it from the bottom up Student needs a new address Instead of really

What is the best way of representing an immutable list in .NET?

天大地大妈咪最大 提交于 2019-12-10 15:19:57
问题 I've recently started using F# for "real work" and rediscovered the beauty of immutable data structures such as the discriminated unions and records in F#. I've also found them to be quite straight forward to use from C#, especially as they don't require any direct dependencies on the F# runtime. However, when it comes to representing lists in these structures, I have not yet found an ideal solution. My first attempt was to type the lists as seq<'a> (IEnumerable in the C# world) which

'Series' objects are mutable, thus they cannot be hashed error calling to_csv

梦想的初衷 提交于 2019-12-10 14:52:13
问题 I have a large Dataframe (5 days with one value per second, several columns) of which I'd like to save 2 columns in a csv file with python pandas df.to_csv module. I tried different ways but always get the error message: 'Series' objects are mutable, thus they cannot be hashed which I found a solution for in connection with groupby but not with filesaving. Somebody has an idea for me? Here a part of my Dataframe: DateTime 2015-07-14 00:00:00 414.37 2015-07-14 00:00:00 414.37 2015-07-14 00:00

Is there an alternative or way to have Rc<RefCell<X>> that restricts mutability of X?

孤街醉人 提交于 2019-12-10 13:43:29
问题 For example given this code: use std::rc::Rc; use std::cell::RefCell; // Don't want to copy for performance reasons struct LibraryData { // Fields ... } // Creates and mutates data field in methods struct LibraryStruct { // Only LibraryStruct should have mutable access to this data: Rc<RefCell<LibraryData>> } impl LibraryStruct { pub fn data(&self) -> Rc<RefCell<LibraryData>> { self.data.clone() } } // Receives data field from LibraryStruct.data() struct A { data: Rc<RefCell<LibraryData>> }

Is it possible for pure functions in Haskell to mutate local copies of variables?

五迷三道 提交于 2019-12-10 13:38:59
问题 Is it possible for pure functions in Haskell to mutate local copies of variables, in the way that clojure can as mentioned in Functional Programming Is A Scam!, by David Nolen? If not what are the reasons for this, and if so are there any examples anyone could point me to? A similar question was asked in Functions that look pure to callers but internally use mutation and the general consensus seemed to be that it was OK for pure functions to perform mutation, as long as the mutations were

How do I create an empty immutable Scala map in Java?

冷暖自知 提交于 2019-12-10 12:46:42
问题 This should be an obvious one but I've not yet found an elegant solution. For various reasons, I need to create an immutable Scala map (scala.collection.immutable.Map from Scala 2.10), but I can only write Java code. How do I do this? 回答1: Wild guess - here goes nothing: scala.collection.immutable.Map$.MODULE$.<…, …>empty() 回答2: At first I was puzzled that just doing the following would fail: scala.collection.immutable.Map<Integer, String> = scala.collection.immutable.Map$.<Integer, String

No Scala mutable list

强颜欢笑 提交于 2019-12-10 12:37:44
问题 Scala has both a mutable and an immutable Map , but it has only an immutable List. If you want a mutable List you need a ListBuffer. I don't understand why this is so. Any one knows?. 回答1: You can choose between these: scala.collection.mutable.DoubleLinkedList scala.collection.mutable.LinkedList scala.collection.mutable.ListBuffer scala.collection.mutable.MutableList So, yes, Scala has mutable lists :-) 回答2: I hope that this article may be of some use to you. The diagram at the bottom of the

Performance of Guava's ImmutableSet.contains

五迷三道 提交于 2019-12-10 12:32:36
问题 Guava's ImmutableSet seems to perform quite poorly in my benchmark concerning contains . For some sizes it gets even much slower than List : size benchmark ns linear runtime 100000 ListContains 110279.54 == 100000 SetContains 7.15 = 100000 ImmutableSetContains 76716.47 = 200000 ListContains 275367.66 ===== 200000 SetContains 7.34 = 200000 ImmutableSetContains 322185.50 ====== 500000 ListContains 935210.10 ==================== 500000 SetContains 7.79 = 500000 ImmutableSetContains 1382765.76 ==

How to subclass int and make it mutable

时光总嘲笑我的痴心妄想 提交于 2019-12-10 12:06:19
问题 Is it possible to subclass int and make it mutable? Consider the following class: class CustomBitVector(int): # bit 7 @property def seventh_property(self): return bool(self & (1 << 7)) @seventh_property.setter def seventh_property(self, val): self |= bool(val) << 7 # bit 6 @property def sixth_property(self): return bool(self & (1 << 6)) @sixth_property.setter def sixth_property(self, val): self |= bool(val) << 6 # ... a few more of these ... # bit 0 @property def zeroth_property(self): return