Code Explainers

Code explainers tagged #immutability

ruby
class KeyTransformer
  def self.camelize(data)
    new.camelize(data)
  end

Recursively camelizing nested Ruby data

recursion data-transformation pattern-matching
Intermediate 7 steps
typescript
import { Injectable, computed, effect, signal } from '@angular/core';
 
export interface CartLine {
  id: string;

A signal-based cart store in Angular

signals reactivity computed-state
Intermediate 8 steps
php
<?php
 
declare(strict_types=1);
 

Streaming a fixed-width file into typed objects

generators value-objects file-parsing
Intermediate 8 steps
java
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Currency;
 

A safe Money value type in Java

immutability bigdecimal value-object
Intermediate 10 steps
python
from copy import deepcopy
 
 
class CowSnapshot:

Copy-on-write attribute snapshots in Python

copy-on-write descriptors attribute-interception
Advanced 7 steps
php
<?php
 
namespace App\Http\Requests;
 

A validated date-range value object in PHP

value-object validation immutability
Intermediate 7 steps
go
package config
 
import "time"
 

The functional options pattern in Go

functional-options closures immutability
Intermediate 7 steps
javascript
import { useState, useRef } from "react";
 
export function TagInput({ initialTags = [], onChange }) {
  const [tags, setTags] = useState(initialTags);

Building a tag input in React

controlled-inputs state-management keyboard-handling
Intermediate 8 steps
python
from copy import deepcopy
from typing import Any, Mapping
 
 

How a recursive deep merge works in Python

recursion immutability dictionaries
Intermediate 6 steps
javascript
function deepFreeze(obj) {
  const propNames = Reflect.ownKeys(obj);
 
  for (const name of propNames) {

Recursively freezing a nested object

recursion immutability object-freezing
Intermediate 6 steps
ruby
class QueryProxy
  ALLOWED = %i[where limit offset order includes distinct].freeze
 
  def initialize(relation, operations = [])

Building a lazy query proxy in Ruby

metaprogramming method_missing lazy-evaluation
Advanced 7 steps
php
final class Route
{
    private string $regex;
    private array $paramNames = [];

Compiling route patterns into regex in PHP

routing regex parsing
Intermediate 8 steps
java
public final class DeepCopier {
 
    private static final ObjectMapper MAPPER = new ObjectMapper()
            .registerModule(new JavaTimeModule())

Deep-copying objects via Jackson serialization

serialization deep-copy generics
Intermediate 7 steps
ruby
module DeepImmutable
  module_function
 
  def deep_freeze(obj)

Deep-freezing config with recursive immutability

immutability recursion deep-copy
Intermediate 8 steps
java
package com.example.time;
 
import java.time.Duration;
import java.time.Instant;

Adding ISO-8601 durations across time zones in Java

date-time iso-8601 parsing
Intermediate 7 steps
java
public final class HttpHeaders {
 
    private final NavigableMap<String, List<String>> values =
            new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

A case-insensitive HTTP headers map in Java

case-insensitivity multimap immutability
Intermediate 7 steps
php
<?php
 
namespace App\Reminders;
 

Scheduling reminders across time zones in PHP

timezones datetime immutability
Intermediate 7 steps
java
public record ServerConfig(
        String host,
        int port,
        Duration timeout,

Loading typed config into a Java record

records factory-methods parsing
Intermediate 7 steps