immutable

Python, subclassing immutable types

I've the following class: class MySet(set): def __init__(self, arg=None): if isinstance(arg, basestring): arg = arg.split() set.__init__(self, arg) This works as expected (initialising the set with the words of the string rather than the letters). However when I want to do the same with the immutable ...

Immutable queue in Clojure

What is the best way to obtain a simple, efficient immutable queue data type in Clojure? It only needs two operations, enqueue and dequeue with the usual semantics. I considered lists and vectors of course, but I understand that they have comparatively poor performance (i.e. O(n) or worse) for modifications at the end and beginning re...

Managing updates to nested immutable data structures in functional languages

I've noticed while on my quest to lean functional programming that there are cases when parameter lists start to become excessive when using nested immutable data structures. This is because when making an update to an object state, you need to update all the parent nodes in the data structure as well. Note that here I take "update" to m...

Java immutable-class rules

Hi, Is the below class immutable: final class MyClass { private final int[] array; public MyClass(int[] array){ this.array = array; } } ...

Immutable class?

Hi, How to make a java class immutable and what is the need of immutability and is there any advantage over this? ...

How to convert generator or iterator to list recursively

I want to convert generator or iterator to list recursively. I wrote a code in below, but it looks naive and ugly, and may be dropped case in doctest. Q1. Help me good version. Q2. How to specify object is immutable or not? import itertools def isiterable(datum): return hasattr(datum, '__iter__') def issubscriptable(datum): ...

Converting immutable to mutable collections

What is the best way to convert collection.immutable.Set to collection.mutable.Set? ...

How to make two directory-entries refer always to the same float-value

Consider this: >>> foo = {} >>> foo[1] = 1.0 >>> foo[2] = foo[1] >>> foo {1: 0.0, 2: 0.0} >>> foo[1] += 1.0 {1: 1.0, 2: 0.0} This is what happens. However, what I want would be that the last line reads: {1: 1.0, 2: 1.0} Meaning that both refer to the same value, even when that value changes. I know that the above works the way it ...

Does the immutable principle still apply when manipulating a char in a string?

If I manipulate a specific char off of a string, would it still be considered as a string manipulation internally by CLR, resulting in temporary string creation? For example : string myString = "String"; myString[0] = 's'; How about creating a char array[] eqvivalent of the string being edited and perform all position specific manipu...

What does immutable mean?

If a string is immutable, does that mean that.... (let's assume JavaScript) var str = 'foo'; alert(str.substr(1)); // oo alert(str); // foo Does it mean, when calling methods on a string, it will return the modified string, but it won't change the initial string? If the string was mutable, does that mean the 2nd alert() would retur...

Types for which "is" keyword may be equivalent to equality operator in Python

For some types in Python, the is operator seems to be equivalent to the == operator. For example: >>> 1 is 1 True >>> "a spoon" is "a spoon" True >>> (1 == 1) is (2 == 2) True However, this is not always the case: >>> [] == [] True >>> [] is [] False This makes sense for immutable types such as lists. However, immutable types suc...

How can I write a multi-client server that doesn't have mutable state?

I am looking at functional programming and struggling with one point.. How do I do the following without mutable state? Imagine I have a server.. and clients try to connect.. and each client gives the server a number and gets told the current total. Now without mutable state the server can't keep a total... so I am thinking each client...

Immutable data structures performance

I don't get how can something as a Set be immutable and still have an acceptable performance. From what I've read in F# Sets internally use Red Black Trees as their implementation. If each time we want to add something new to a Red Black Tree we have to basically recreate it, how can it have ever good performance? What am I missing her...

How do I use a setter instead of a constructor for a final variable?

I am parsing an XML file where one of the fields I want to be immutable, ID, has to be set after the object is created. Should I set it to null, and throw an exception in the setID() method if ID!=null ? Edit: I am parsing an XML file, and at the beginning, I create an object whose fields and objects are populated using information in X...

Implement an immutable deque as a balanced binary tree?

I've been thinking for a while about how to go about implementing a deque (that is, a double-ended queue) as an immutable data structure. There seem to be different ways of doing this. AFAIK, immutable data structures are generally hierarchical, so that major parts of it can be reused after modifying operations such as the insertion or ...

Examples of open-source code which contain extensive examples of Immutable classes?

I am looking for realistic examples of immutable classes developed in Java. In order to allow finding those classes which are intended to be immutable, it would be helpful if the code used an @Immutable annotation, or otherwise documented classes as intended to be immutable. I also need to be able to study the source code of those classe...

Scala DSL without extra syntax

Hey, I asked myself this question a couple of times and came up with a solution for that feels very dirty. Maybe you can give me any advice since I think this is a basic problem for every DSL written in Scala. I want to have a hierarchical structure of nested objects without adding any extra syntax. Specs is a good example for this: M...

How can I find which property getters have side effects using NDepend?

A familiar problem using VisualStudio is the mysterious calling of property getters. If these have side effects (the most common being of the form if (foo == null) foo = new foo(); return foo; ), then the fact that the debugger Locals and Watch windows call the properties - without even hitting any break points - can lead to unexpected e...

Immutable game object, basic functional programming question.

Hi all, I'm in the process of trying to 'learn more of' and 'learn lessons from' functional programming and the idea of immutability being good for concurrency, etc. As a thought exercise I imagined a simple game where Mario-esq type character can run and jump around with enemies that shoot at him... Then I tried to imagine this bein...

Why does wrapper classes for primitive data types don't have a setter ?

What is the reason why Wrapper classes (like Integer, Double, etc.) don't have a setter for their inner primitive value ? I am asking this because that kind of functionality would have simplified calculus, and have made the Java language a little more flexible . Let me give you some examples. 1) Let's take the following example: Int...