immutability

Implementing a content-hashable HashSet in C# (like python's `frozenset`)

若如初见. 提交于 2019-12-14 03:27:34
问题 Brief summary I want to build a set of sets of items in C#. The inner sets of items have a GetHashCode and Equals method defined by their contents . In mathematical notation: x = { } x.Add( { A, B, C } ) x.Add( { A, D } ) x.Add( { B, C, A } ) now x should be{ { A, B, C }, { A, D } } In python, this could be accomplished with frozenset : x = set() x.add( frozenset(['A','B','C']) ) x.add( frozenset(['A','D']) ) x.add( frozenset(['B','C','A']) ) /BriefSummary I would like to have a hashable

Push array into array on ruby by just one level

只谈情不闲聊 提交于 2019-12-14 02:20:08
问题 Given: a = [[1,"a"],[2,"b"]] b = [[3,"c"],[4,"d"]] I want to turn a into [[1,"a"],[2,"b"][3,"c"],[4,"d"]] . How can do this without + ? It creates a new array, which I want to avoid. (a << b).flatten(1) # => [1, "a", 2, "b", [3, "c"], [4, "d"]] 回答1: a.concat(b) ............................... 回答2: a = [[1,"a"],[2,"b"]] b = [[3,"c"],[4,"d"]] a[a.length, 0] = b a # > [[1, "a"], [2, "b"], [3, "c"], [4, "d"]] 回答3: concat is the answer, but you could do this: a.object_id #=> 70223889895340 a

Is Java String immutable? [duplicate]

一个人想着一个人 提交于 2019-12-13 23:21:26
问题 This question already has answers here : Immutability of Strings in Java (26 answers) Is Java “pass-by-reference” or “pass-by-value”? (86 answers) Closed 6 years ago . I don't understand this code, why my string c don't changing in main method but changing in changeString . Can you explain me? class MainClass { public static void main(String[] args) { String c = "lalala"; changeString(c); System.out.println("str in main = "+c); } public static void changeString(String str) { str = str +

How to modify a value of a Map which contains Sets, returning a new Map?

杀马特。学长 韩版系。学妹 提交于 2019-12-13 18:59:08
问题 Given a Map[Int, Set[Int]] , how can I modify a single value of the Map, generating a new one in the process, for example: val x = Map(1 -> Set(1,2,3)) x(1) + 5 // This creates a new Set, but not a new Map val y = x(1) change { x => x + 5 } // The previous functionality is what I'm looking for // z: Set[Int]] = List(Set(1, 2, 3, 5)) 回答1: In scala 2.10: implicit class ChangeableMap[K,V]( val m: Map[K,V] ) extends AnyVal { def change( k: K )( transform: V => V ): Map[K,V] = { m.get( k ).map{ v

Immutable Dictionary<TKey, TValue>

不羁的心 提交于 2019-12-13 17:52:58
问题 How would you implement constructors for an immutable Dictionary<TKey, TValue> -like class? Also, is it possible to allow users to use the syntax: ImmutableDic<int, int> Instance = new ImmutableDic<int, int> { {1, 2}, {2, 4}, {3,1} }; 回答1: The simplest solution is to write a constructor that accepts a mutable IDictionary<TKey, TValue> . Build the mutable dictionary and just pass it to the constructor of your immutable dictionary: var data = new Dictionary<int, int> { {1, 2}, {2, 4}, {3,1} };

How to generate stable id for AST nodes in functional programming?

回眸只為那壹抹淺笑 提交于 2019-12-13 14:05:24
问题 I want to substitute a specific AST node into another, and this substituted node is specified by interactive user input. In non-functional programming, you can use mutable data structure, and each AST node have a object reference, so when I need to reference to a specific node, I can use this reference. But in functional programming, use IORef is not recommended, so I need to generate id for each AST node, and I want this id to be stable , which means: when a node is not changed, the

LINQ to SQL and immutability

ぐ巨炮叔叔 提交于 2019-12-13 13:07:42
问题 I'm tryign to use LINQ to SQL. Everythign works great but it seems that it is not friendly to immutable objects. LINQ to SQL requires me to create a parameterless constructor for my immutable class. But I want it to be immutable, so I want to only allow people to create it with all the required parameters each time. Likewise I need to have setters on all of my members. Is there a way that I can still use LINQ 2 SQL without giving up my immutability? Here is my code: DataContext db = new

Are all immutable objects singleton instances? [closed]

泪湿孤枕 提交于 2019-12-13 11:16:44
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . Are all immutable objects singleton instances? 回答1: No. Immutable objects are unchangeable, the normal practice for these is to pass (inject) all required values via the constructor (i.e. they are specified when

How do I modify static properties of a class in PHP?

丶灬走出姿态 提交于 2019-12-13 10:29:34
问题 Consider the following code: class MyClass { public static $test = 'foo'; public function example() { return Self::$test; } } // What I'm trying to do MyClass->$test = 'bar'; $test = new MyClass(); echo $test->example(); // Should return `bar` instead of `foo`. Is this or anything remotely close to this possible in PHP? 回答1: You're on the right track you just need to access the variable as Class::$test class MyClass { public static $test = 'foo'; public function example() { return Self::$test

Why is this implementation of a queue with two stacks immutable and thread safe?

99封情书 提交于 2019-12-13 07:12:18
问题 I've seen this way of implementing a queue with two stacks: https://stackoverflow.com/a/2050402/494094 And I've read that this way the queue is immutable and thread-safe. What's the point that separates this from a normal queue and makes it immutable and thread-safe? I'd really appreciate it if someone could explain it in a simple, non-professional way. 回答1: How to implement a queue using two stacks? explains more and has some code. If you do this in a low-level way you will have two memory