immutability

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

Do effectively immutable objects make sense?

最后都变了- 提交于 2019-12-04 01:59:24
In the book Java Concurrency In Practice it explains the advantages of "effectively immutable" objects versus mutable objects concurrency-wise. But it does not explain what advantage "effectively immutables" objects would offer over really immutable objects. And I don't get it: can't you always build a really immutable object at the moment you'd decide to publish safely an "effectively immutable" object? (instead of doing your "safe publication" you'd build a really immutable object and that's it) When I'm designing classes I fail to see cases where I couldn't always build a truly immutable

enum vs immutable in D

谁都会走 提交于 2019-12-04 01:45:49
What's the difference between enum i = 2; enum s = "Hello"; and immutable i = 2; immutable s = "Hello"; in D 2.0? stephan An enum is a user-defined type, not a variable. enum e = 2; is a short-hand for something like this enum : int { e = 2 } (i.e. an anonymous enum with one member e ), see the documentation . By definition, all members of an anonymous enum are placed into the current scope. So, e is a type member placed into the current scope, where it behaves like a literal . immutable i = 2; on the other hand actually creates a variable i of type int. This difference has a couple of

Why instance variable to be final?

假如想象 提交于 2019-12-04 01:35:03
问题 I read this question about immutable objects and was left with a question regarding immutable objects and final field: Why do we need instance variable in immutable class to be final? For example, consider this immutable class: public final class Immutable { private final int someVal; public Immutable(int someVal) { this.someVal= someVal; } public int getVal() { return val; } } If in the above code there is no set methods and the instance variable is set only within the constructor, why is it

assign “it” in each iteration (groovy)

可紊 提交于 2019-12-04 01:33:13
Hey, i try to trim each string item of an list in groovy list.each() { it = it.trim(); } But this only works within the closure, in the list the strings are still " foo", "bar " and " groovy ". How can i achieve that? list = list.collect { it.trim() } You could also use the spread operator: def list = [" foo", "bar ", " groovy "] list = list*.trim() assert "foo" == list[0] assert "bar" == list[1] assert "groovy" == list[2] According to the Groovy Quick Start , using collect will collect the values returned from the closure. Here's a little example using the Groovy Shell: groovy:000> ["a ", " b

immutable in F#

喜欢而已 提交于 2019-12-04 00:55:41
问题 I know that variables in F# are immutable by default. But, for example in F# interactive: > let x = 4;; val x : int = 4 > let x = 5;; val x : int = 5 > x;; val it : int = 5 > So, I assign 4 to x, then 5 to x and it's changing. Is it correct? Should it give some error or warning? Or I just don't understand how it works? 回答1: When you write let x = 3 , you are binding the identifier x to the value 3 . If you do that a second time in the same scope, you are declaring a new identifier that hides

Is there a way to freeze an ES6 Map?

情到浓时终转凉″ 提交于 2019-12-04 00:49:48
I'm looking for a way to freeze native ES6 Maps. Object.freeze and Object.seal don't seem to work: let myMap = new Map([["key1", "value1"]]); // Map { 'key1' => 'value1' } Object.freeze(myMap); Object.seal(myMap); myMap.set("key2", "value2"); // Map { 'key1' => 'value1', 'key2' => 'value2' } Is this intended behavior since freeze freezes properties of objects and maps are no objects or might this be a bug / not implemented yet? And yes I know, I should probably use Immutable.js , but is there any way to do this with native ES6 Maps? There is not, you could write a wrapper to do that. Object

When manipulating immutable datastructures, what's the difference between Clojure's assoc-in and Haskell's lenses?

穿精又带淫゛_ 提交于 2019-12-04 00:20:47
I need to manipulate and modify deeply nested immutable collections (maps and lists), and I'd like to better understand the different approaches. These two libraries solve more or less the same problem, right? How are they different, what types of problem is one approach more suitable for over the other? Clojure's assoc-in Haskell's lens Clojure's assoc-in lets you specify a path through a nested data struture using integers and keywords and introduce a new value at that path. It has partners dissoc-in , get-in , and update-in which remove elements, get them without removal, or modify them

Why are Scala's `Lists` implemented as linked lists

孤街醉人 提交于 2019-12-04 00:18:31
I always thought that the benefit of linked lists was that you could add or remove items (especially not from the end) without having to copy lots of elements thanks to the beauty of pointers. However, Scala's List is immutable (at least by default). What is the benefit of having an immutable linked list (because there are definite downsides, e.g. not O(1) element access.) Thanks! I think the main reason is that one of the most powerful uses of linked lists is head/tail splitting. There are lots of recursive algorithms that look like this one: def listlen[A](xs: List[A], already: Int = 0): Int

VB.NET linq group by with anonymous types not working as expected

吃可爱长大的小学妹 提交于 2019-12-04 00:12:47
问题 I was toying around with some of the linq samples that come with LINQPad. In the "C# 3.0 in a Nutshell" folder, under Chater 9 - Grouping, there is a sample query called "Grouping by Multiple Keys". It contains the following query: from n in new[] { "Tom", "Dick", "Harry", "Mary", "Jay" }.AsQueryable() group n by new { FirstLetter = n[0], Length = n.Length } I added the string "Jon" to the end of the array to get an actual grouping, and came up with the following result: This was exactly what