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
3
votes
1answer
58 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 ...
9
votes
2answers
242 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 ...
0
votes
1answer
54 views

Builder pattern in C# supporting subclassing with nested classes [closed]

[Posted yesterday on Software Engineering, but was apparently "disappeared"...maybe better here] Background: I am just starting to get my head around the idea of separating the domain model from the ...
7
votes
1answer
100 views

Quicksort in JavaScript assuming an immutable array

Update: further succinct versions below (inspired by Haskell) Quick sort in JS assuming an immutable array: ...
3
votes
2answers
90 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 ...
4
votes
2answers
92 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 ...
1
vote
0answers
54 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
63 views

Immutable employee class

I have created an employee object to be immutable. ...
8
votes
3answers
240 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
178 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
37 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
154 views

Rust: Flattening nested struct to Vec<T>

I have a struct which nests other structs like following: ...
1
vote
0answers
76 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
137 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
72 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
65 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
183 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
114 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 ...
8
votes
4answers
667 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
95 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
199 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
98 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
2k 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
192 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
192 views

My attempt at an immutable container view in C++

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

Implement array element swap in a functional language

Sorting algorithms involve swapping element positions in an array. This is my implementation of swap: ...
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 ...
1
vote
0answers
47 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
985 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 ...
2
votes
1answer
204 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
96 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
703 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
328 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
517 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
197 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
339 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
85 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
165 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
141 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 ...
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 ...
0
votes
2answers
7k 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 ...
16
votes
4answers
23k 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
639 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
188 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
136 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
707 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
192 views

Creating SOLID immutable and mutable Location types

I have a Location type: ...