Questions tagged [immutability]

Immutability is the inability to modify data after it has been created. Modifications are instead made by copying the data. A property of immutable data is that it is *referentially transparent*.

Filter by
Sorted by
Tagged with
37
votes
8answers
4k views

Immutable objects in Java

I just finished studying immutable objects and their advantages so I thought I'd create one of my own. Here is my employee class that extends a person class. It is not mutable since I have getters to ...
18
votes
3answers
3k views

Really simple immutable class

I have a User entity. Each instance of the User class should have a first name, surname and email, and these shall never be ...
18
votes
4answers
25k views

Simple builder pattern implementation for building immutable objects

This is a builder pattern implementation to build immutable Person objects: Person class ...
12
votes
3answers
1k views

Immutable Object class in VBA – Creatable only through constructor and not via “New” keyword

Goals for the class Create Immutable objects – i.e. only Getters – no Setters Object creation only possible through a constructor, not via New keyword, to ensure ...
12
votes
1answer
332 views

Cat fight (with rockets)

My code is attempting to implement FP in an efficient / readable manner into some of my video games. I realize this may be a bit subjective, but I feel there is enough merit / objectivity to be of ...
11
votes
3answers
3k views

Initializing immutable objects with a nested builder

I need to initialize several parameters of an object but I also wanted to make it immutable. At the same time I don't want the constructor to take all several parameters because some of them are ...
10
votes
2answers
260 views

Immutable Fraction class

Inspired by this question: Fraction class implemented in Objective-C, I have written what I feel is an improved version of the Fraction class in Objective-C. As per the tips in this answer, the class ...
9
votes
4answers
4k views

Creating unique registration numbers using object factories

I'm learning about immutability and uniqueness in Java, particularly the use of defensive programming and object factories. I have been asked to create a class containing an object factory that when ...
9
votes
2answers
261 views

Immutable builder and updater

There aren't enough questions about creating immutable objects... so why not try it again with another approach. This time, it's a builder that maps properties to constructor parameters. Properties ...
9
votes
2answers
883 views

Immutable Linked List in VBA

I made an immutable list class using the head-tail idiom. If I did this correctly, it implements persistent data structures. Unfortunately it doesn't scale well ...
8
votes
4answers
811 views

Selective updates to immutable types

There is a way in Scala to selectively change a field while making a copy of the immutable object: ...
8
votes
3answers
2k views

ImmutableList implementation in C#

I tried to implement an immutable list. ...
8
votes
3answers
476 views

Stack as a Persistent Data Structure Implementation

After reading some articles about immutability, I found out that there are persistent data structures. In the following, I implemented a stack as a persistent data structure. What I like about the ...
8
votes
2answers
425 views

Declaration of bloatedness: the class that knew too much?

I've already expressed concerns about this type's constructor, and I've sort-of* implemented the changes suggested in the answers I got there. Recently I added even more parameters to that ...
8
votes
2answers
155 views

Inheriting methods of an immutable type

In my project, I have a type BalanceByBucket that is an immutable type and has a bunch of methods. I have another class called a ...
7
votes
1answer
115 views

Quicksort in JavaScript assuming an immutable array

Update: further succinct versions below (inspired by Haskell) Quick sort in JS assuming an immutable array: ...
7
votes
1answer
172 views

TCPListener server to discover clients on a network

I am currently writing a program to sync files (music, photos, etc) from my PC to an Android device. In order to do this, I have 2 application: one that is running on my PC, and one that shall be ...
7
votes
0answers
100 views

Agent-based Immutable Map

I am in the process of removing the last few pieces of mutable state from an F#-based distributed system. Some of the remaining mutable state is a ...
6
votes
4answers
7k views

Should I copy list both in constructor and in getter?

I have a simple immutable class: ...
6
votes
2answers
849 views

Generic immutable object builder

I've made an object builder which can create all sorts of classes, it can also create immutable objects. There are 2 requirements that must be met in order for this pattern to work with fully ...
6
votes
4answers
192 views

Creating SOLID immutable and mutable Location types

I have a Location type: ...
6
votes
3answers
42k views

Insert a character into a string

For practicing reasons I would like to write a method which inserts a character into a string. I would like to know: What is the best practice concerning placement of a comment within methods? For ...
6
votes
1answer
204 views

My attempt at an immutable container view in C++

I have this very simple template: ...
6
votes
2answers
206 views

Implementation of a Location object, to be used in building a text-based adventure game

Based on responses to my question posted here, I've built a full implementation of a Location object. Such an object can be used to build a map for a text-based adventure game. Feel free to review ...
6
votes
1answer
419 views

Circular dependencies between immutable objects; the Freeze Pattern

Generally, I structure small threadsafe immutable objects like this: ...
6
votes
1answer
2k views

How is my implementation of an immutable stack?

Below is my implementation of an immutable stack class. The reverse function is trying to return a stack with all elements reversed. Is my implementation good? Maybe the reverse function can be ...
6
votes
1answer
163 views

Allow changing the properties of a mutable controller in a thread safe way

I have a (control engineering) controller. These controllers usually need several parameters to do their thing, and in my application it is desirable that these parameters can be changed while the ...
5
votes
1answer
274 views

Immutable Matrix

I'm writing implementations of some numerical methods to solve linear equations systems, those implementations use the following Matrix class. I'm trying to get this class immutable, due to that the ...
5
votes
3answers
115 views

Generating a game board with squares in a rectangle

I am trying to generate a game board (similar to a Monopoly board). I am new to Scala and am thinking that there may be a better way to do this. But improvements to the code are also welcome. ...
5
votes
1answer
147 views

Immutable File Upload Server Configuration Class (FTP or Network Drive)

The application I'm working on needs to occasionally take pictures, and then send them to either an ftp server or a network drive (user configurable). To try to abstract away the differences between a ...
5
votes
2answers
1k views

Functions with mutable and non-mutable named tuples

I am making some functions within a function for learning which when passed information about a named tuple: it returns a reference to a class object from which we can construct instances of the ...
5
votes
1answer
806 views

Immutable C++ stack - thoughts and performance

What are your thoughts on the fallowing immutable stack implementation? It is implemented having as a basis a C# immutable stack (where garbage collector assistance does not impose using a reference ...
5
votes
1answer
685 views

Yet another immutable string

I know, there are a few implementations of immutable strings out there, but my focus seems to be a little different. My goal was to have a type that provided value semantics, but didn't incur the ...
4
votes
2answers
2k views

Immutable Queue in Java using an Immutable Stack

I am new to this Immutability concept and I have some coding experience in JAVA. Recently as a part of internship program, they gave me a 5-day task, which was to implement Immutable Queue. After some ...
4
votes
2answers
155 views

Counting valleys traversed below sea level, given elevation changes

Problem Statement: Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly steps. For every step ...
4
votes
1answer
83 views

Immutable type updater using a special constructor

I wanted to update some properties of my UriString but since this type is immutable, it wouldn't work. Actually, I just wanted to update the Path property but I don'...
4
votes
1answer
870 views

Functional linked list

I've been implementing functional linked lists in different languages, just as a learning exercise. I'm looking for feedback in general: code correctness, best practices, design patterns; everything ...
4
votes
2answers
887 views

Immutable pure data classes with public setters on properties

I'm putting together some classes as a model for some information (that I'm currently pulling from a website). The classes are implemented in C# - because in the current version of F# there are no ...
4
votes
1answer
72 views

Immutable Keyed Set

Description An immutable keyed set is a readonly collection of elements without duplicates or null values having each of the elements linked to exactly one key. Example This simple example ...
4
votes
1answer
187 views

Heterogenous persistent list in Rust

I have implemented a basic, but seemingly working, persistent (as in immutable), heterogenous list type in Rust. Here's the code: ...
4
votes
1answer
129 views

Lock-free Immutable ConcurrentQueue

Similar to the code review I posted last week for an agent-based immutable replacement for ConcurrentDictionary, I have also created an agent-based immutable ...
4
votes
0answers
70 views

Functional implementation of the Maybe Monad in JS [closed]

A while ago, I really wanted to have the Maybe Monad in my JS projects. I ended up trying to use Promises as a stand-in solution (using Promise.resolve as my unit, ...
3
votes
5answers
205 views

An immutable person

I am trying to model person in OOPS using immutability. I have created an object bond with age as 25. To change the age I have created a new object and returned it. ...
3
votes
2answers
257 views

Implement Immutable Collection in C#

I would like to define some constant sequences (of bytes, specifically) in my C# library/API. Both length and content should not vary, preferably by any means (barring reflection). To this end, a <...
3
votes
2answers
177 views

Count character 'a' in first n characters of indefinitely repeating string s

Problem statement: Lilah has a string, s, of lowercase English letters that she repeated infinitely many times. Given an integer, n, find and print the number of letter a's in the first n ...
3
votes
1answer
98 views

Non-persistent storage for key-value pairs

I got an interesting interview question related to JavaScript: In JavaScript please create a storing function called storingFn with the following characteristics:...
3
votes
1answer
579 views

Subclassing `int` to make colors

In the ROOT framework (originally a C++ framework, but python bindings are also supplied), colors are objects (ROOT.TColor) identified with an integer index. This ...
3
votes
1answer
596 views

Creating Proxy classes for immutable objects

As a bit of a learning project, I decided to take the concept of proxying objects a bit further and extend it to creating proxy classes which create proxy'd objects. I originally found the idea of ...
3
votes
1answer
175 views

Functional SaveObject in Scala

I came across Java like Scala code that I am trying to refactor to make it functional and immutable. The first obvious flaw is that it's violating thread safety by using mutable public class level ...
2
votes
3answers
1k views

ObservableQueue

I'm looking for feedback on this. ...