Code Explainers

Code explainers tagged #state-machine

rust
pub fn extract_quoted_fields(line: &str) -> Vec<String> {
    let mut fields = Vec::new();
    let mut current = String::new();
    let mut in_quotes = false;

Parsing quoted fields with a state machine in Rust

state-machine parsing string-processing
Intermediate 7 steps
rust
use std::sync::Mutex;
use std::time::{Duration, Instant};
 
#[derive(Debug, Clone, Copy, PartialEq)]

Building a circuit breaker in Rust

circuit-breaker state-machine concurrency
Advanced 8 steps
ruby
require 'set'
 
class CronSchedule
  FIELDS = %i[minute hour day month wday].freeze

Parsing and matching cron expressions in Ruby

parsing time-arithmetic sets
Intermediate 10 steps
java
public final class IniParser {
 
    private static final Pattern SECTION = Pattern.compile("\\[(.+?)\\]");
 

Parsing INI files in Java

parsing regex state-machine
Intermediate 10 steps
typescript
interface ParsedCookie {
  name: string;
  value: string;
  domain?: string;

Parsing a Set-Cookie header in TypeScript

parsing string-manipulation type-safety
Intermediate 9 steps
go
package config
 
import (
	"bufio"

Parsing INI files in Go

parsing state-machine error-handling
Intermediate 7 steps
java
public final class CircuitBreaker {
 
    private enum State { CLOSED, OPEN, HALF_OPEN }
 

How a circuit breaker guards failing calls

state-machine resilience concurrency
Advanced 7 steps
rust
use std::collections::HashMap;
 
#[derive(Clone, Copy, PartialEq)]
enum Color {

Detecting cycles with three-color DFS in Rust

graph-algorithms cycle-detection depth-first-search
Intermediate 9 steps
rust
use std::time::Duration;
 
#[derive(Debug, PartialEq)]
pub enum ParseDurationError {

Parsing duration strings safely in Rust

parsing error-handling checked-arithmetic
Intermediate 8 steps
go
package logparse
 
import (
	"bufio"

Splitting multi-line logs with a Scanner

parsing streaming bufio
Intermediate 9 steps
typescript
type TokenType = "keyword" | "string" | "comment" | "number" | "text";
 
interface Token {
  type: TokenType;

How a regex tokenizer highlights code

tokenizer regex lexing
Intermediate 10 steps
rust
use std::time::{Duration, Instant};
 
pub struct Ewma {
    alpha: f64,

A time-decayed moving average in Rust

exponential-smoothing time-decay state-machine
Intermediate 8 steps
go
package breaker
 
import (
	"errors"

How a circuit breaker works in Go

circuit-breaker state-machine concurrency
Intermediate 8 steps
rust
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
    Number(f64),
    Plus,

How a tokenizer turns text into tokens

lexing enums iterators
Intermediate 8 steps
php
<?php
 
final class MarkdownParser
{

Building a small Markdown-to-HTML parser in PHP

state-machine parsing closures
Intermediate 10 steps
javascript
import { useReducer, useEffect } from "react";
 
const initialState = { status: "idle", data: null, error: null };
 

Building a data-fetching hook in React

custom-hooks usereducer data-fetching
Intermediate 9 steps
typescript
export const OrderStatus = {
  Pending: 'pending',
  Paid: 'paid',
  Shipped: 'shipped',

A type-safe order state machine in TypeScript

state-machine union-types type-guards
Intermediate 8 steps
rust
pub fn collapse_whitespace(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let mut in_whitespace = false;
 

Collapsing runs of whitespace in Rust

string-processing state-machine iteration
Beginner 5 steps