Code Explainers

Code explainers tagged #streams

rust
use axum::{
    extract::{Path, State},
    response::sse::{Event, KeepAlive, Sse},
};

Streaming import progress with SSE in Axum

server-sent-events streams watch-channel
Advanced 7 steps
java
public record TimeSlot(LocalDate day, LocalDateTime start, LocalDateTime end) {}
 
public class ScheduleAnalyzer {
 

Detecting calendar conflicts in Java

streams sorting sweep-line
Intermediate 8 steps
java
public List<Path> expandGlob(Path baseDir, String glob) throws IOException {
    FileSystem fs = baseDir.getFileSystem();
    PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
    boolean recursive = glob.contains("**");

Expanding glob patterns with NIO

glob-matching nio streams
Intermediate 5 steps
java
import java.util.Arrays;
import java.util.List;
 
public final class LatencyStats {

Computing latency percentiles in Java

percentiles interpolation immutability
Intermediate 9 steps
java
public Map<Long, CustomerDto> indexByCustomerId(List<CustomerDto> customers) {
    return customers.stream()
            .collect(Collectors.toMap(
                    CustomerDto::getId,

Building maps from lists with Collectors.toMap

streams collectors maps
Intermediate 5 steps
java
public List<DailySample> backfillMissingDates(List<DailySample> samples, double fillValue) {
    if (samples.isEmpty()) {
        return List.of();
    }

Backfilling gaps in a daily time series in Java

streams time-series treemap
Intermediate 5 steps
java
package com.example.validation;
 
import java.util.List;
import java.util.stream.Collectors;

Validating JSON payloads against a schema in Java

json-schema validation recursion
Intermediate 8 steps
java
public class DuplicateFinder {
 
    public Map<String, List<Path>> findDuplicates(Path root) throws IOException {
        Map<String, List<Path>> byHash = new HashMap<>();

Finding duplicate files by content hash

hashing file-io streams
Intermediate 8 steps
javascript
import { NextResponse } from 'next/server';
import { createWriteStream } from 'node:fs';
import { mkdir } from 'node:fs/promises';
import { pipeline } from 'node:stream/promises';

Streaming file uploads in a Next.js route

file-upload streams validation
Intermediate 9 steps
java
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;

Ranking the most frequent words in Java

streams regex grouping
Intermediate 7 steps
rust
use axum::{
    extract::State,
    response::sse::{Event, KeepAlive, Sse},
};

Broadcasting live prices over SSE in Axum

server-sent-events broadcast-channel streams
Advanced 9 steps
java
public record FixedWidthField(String name, int start, int end) {
 
    public String extract(String line) {
        int from = Math.min(start, line.length());

Parsing fixed-width text in Java

records parsing streams
Intermediate 7 steps
java
@Controller
public class BookController {
 
    private final BookService bookService;

Solving GraphQL N+1 with batch mapping in Spring

graphql n+1-problem batching
Intermediate 8 steps
java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

Compressing byte arrays with GZIP in Java

compression streams try-with-resources
Intermediate 6 steps
java
public final class ZipExtractor {
 
    private final Path targetDir;
 

Extracting a zip safely in Java

path-traversal streams file-io
Intermediate 8 steps
typescript
type JsonValue = Record<string, unknown> | unknown[];
 
export function parseJsonStream<T = JsonValue>(
  stream: ReadableStream<Uint8Array>,

Streaming JSON parsing with a depth counter

streams parsing state-machine
Advanced 9 steps
java
public final class ListPartitioner {
 
    private ListPartitioner() {
    }

Splitting a list into fixed-size chunks in Java

generics partitioning streams
Intermediate 8 steps
java
import java.util.List;
import java.util.IntSummaryStatistics;
 
public class OrderReport {

Aggregating stats with IntSummaryStatistics

streams aggregation records
Intermediate 5 steps