Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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*.

1
vote
0answers
25 views

Near immutable, thread safe Builder

I'm writing a torrent streaming client. The plan is to wrap WebTorrent-cli and provide a Java interface to observe download state, cancel or pause downloads etc. Anyways, I came up with the following ...
2
votes
2answers
52 views

Immutable employee class

I have created an employee object to be immutable. ...
8
votes
3answers
126 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 ...
3
votes
2answers
148 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 <...
1
vote
0answers
31 views

Rust trait implementations for a Blackjack game

I am learning Rust. I have build a simple game (Blackjack) to get acquainted. The following code is the code which runs the game logic. The code is part of the larger codebase and works. In the rest ...
2
votes
1answer
77 views

Rust: Flattening nested struct to Vec<T>

I have a struct which nests other structs like following: ...
1
vote
0answers
63 views

Extension “With” for immutable types

(My code is basically a rewrite of https://github.com/ababik/Remute so much of the credit goes there) The idea is to use lambda expression to provide a general With...
1
vote
0answers
73 views

Max heap in Haskell

I wrote a (max) heap in Haskell which is balanced after every (public) operation. Not based on any documentation. Focus is on: speed, that is the right time-complexities (not necessarily optimal); ...
4
votes
1answer
57 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
0answers
62 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, ...
4
votes
1answer
177 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: ...
3
votes
0answers
88 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 ...
9
votes
4answers
576 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: ...
7
votes
0answers
91 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 ...
1
vote
1answer
153 views

Immutable Object Patterns for Domain Models

I'm trying to introduce immutable types into our code base and I'm trying to determine a good pattern that is intuitive for our team. I was reading about the Popsicle pattern and that seemed ...
1
vote
0answers
85 views

Immutable Queue with Bulk-Enqueue

I recently needed a data structure with FIFO queue semantics that supported efficient bulk-insertion (often need to enqueue thousands of messages at once). I took a look at Purely Functional Data ...
1
vote
2answers
1k views

Creating a Student class for a project

To recover from yesterday's disaster, I re-read the SOLID principles and refactored my code. Hopefully I did a better job than yesterday. I wanted to keep my Student...
1
vote
2answers
181 views

Creating Immutable Student Object

For a project I'm working on I have the following requirements: We have two student types: Domestic and International ...
2
votes
1answer
87 views

Unsafe collection which allows immutable access while also allowing push()

To get myself used to writing unsafe code, I wrote a collection which allows the user to keep immutable references to the elements of a vector while still being ...
6
votes
1answer
181 views

My attempt at an immutable container view in C++

I have this very simple template: ...
2
votes
1answer
506 views

Implement array element swap in a functional language

Sorting algorithms involve swapping element positions in an array. This is my implementation of swap: ...
19
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 ...
1
vote
0answers
46 views

Processing string market data and perform calculations

I would like to know how I can make the following more readable/maintainable, reduce side effects and account for edge cases, unit test cases and things I need to be aware of with extreme values. ...
11
votes
3answers
834 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 ...
3
votes
1answer
174 views

Functional API wrapper written in Scala

I've been working on an small API wrapper for the GitHub API using Scala (full source on GitHub). I'm still very new to the language, so I was hoping to get some feedback about a couple of design ...
3
votes
1answer
95 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:...
6
votes
2answers
585 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 ...
12
votes
1answer
327 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 ...
3
votes
1answer
458 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 ...
6
votes
2answers
189 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 ...
2
votes
2answers
316 views

A Scala Maze Generator in Functional Style

I'm wondering if there is more I can do to incorporate more idiomatic scala and functional programming principles. I know the maze itself is mutable but i didn't see an easy solution to making it ...
2
votes
1answer
83 views

Building and querying an immutable phone book

I wanted to get your opinion on this immutable implementation of a Phone Book using recursion to process a stream. I am especially interested in ways to speed up the code execution. Here is an ...
0
votes
1answer
47 views

Class that holds calculation results and has to be immutable

I'm roughly trying to follow the guidelines in "Effective Java", "Item 15: Minimize mutability", hence the class is not "final" on purpose. Information is gathered from a number of temperature ...
7
votes
1answer
160 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 ...
5
votes
1answer
133 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 ...
9
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 ...
0
votes
2answers
6k views

Returning a byte array from a method [closed]

public abstract class Element { // Removed for verbosity ... protected abstract byte[] GetBytes(); } I'm attempting to create an engine that can create a ...
14
votes
4answers
21k views

Simple builder pattern implementation for building immutable objects

This is a builder pattern implementation to build immutable Person objects: Person class ...
5
votes
1answer
571 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 ...
3
votes
5answers
185 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. ...
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 ...
2
votes
2answers
135 views

Immutable subclass of a Set class

If I'm creating an immutable class from an existing class, should I override methods that mutate the property or have an instance of it and write my own methods to read from the property? I have an ...
1
vote
1answer
688 views

Immutable C++ String class

I have project where I will need to create lots of immutable strings. If I am using std::string, which has huge overhead - about 60-70% against ...
6
votes
4answers
188 views
7
votes
3answers
2k views

ImmutableList implementation in C#

I tried to implement an immutable list. ...
8
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 ...
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 ...
8
votes
2answers
141 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 ...
1
vote
1answer
80 views

Removing getters for a custom enum [closed]

Recently, I perform re-factor on one of my frequent used custom enum, by removing its getter. My rational for doing so it. The enum's custom fields are immutable Only use getters when it is ...
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 ...