Code Explainers

Code explainers tagged #pattern-matching

ruby
class UserAgentParser
  BROWSERS = [
    [/Edg\/([\d.]+)/, "Edge"],
    [/OPR\/([\d.]+)/, "Opera"],

Parsing user-agent strings in Ruby

regex pattern-matching lookup-tables
Intermediate 8 steps
python
import fnmatch
from collections import defaultdict
from typing import Any, Callable
 

A pattern-matching event bus in Python

pub-sub decorators pattern-matching
Intermediate 8 steps
rust
use serde_json::{Map, Value};
 
pub fn merge_config(base: &mut Value, override_with: Value) {
    match (base, override_with) {

Deep-merging JSON config layers in Rust

recursion json pattern-matching
Intermediate 9 steps
go
package useragent
 
import (
	"regexp"

Parsing user-agent strings in Go

regex string-parsing ordering
Intermediate 7 steps
java
public final class UserAgentParser {
 
    private static final Pattern OS_PATTERN = Pattern.compile(
            "(Windows NT [0-9.]+|Mac OS X [0-9_]+|Android [0-9.]+|iPhone OS [0-9_]+|Linux)");

Parsing user-agent strings with regex in Java

regex parsing pattern-matching
Intermediate 9 steps
javascript
const legacyRedirects = [
  { from: '/blog/:slug', to: '/articles/:slug' },
  { from: '/shop/product/:id', to: '/store/items/:id' },
  { from: '/about-us', to: '/about' },

How redirects work in Next.js config

redirects routing configuration
Intermediate 7 steps
php
<?php
 
namespace App\Http;
 

HTTP content negotiation in PHP

http content-negotiation parsing
Intermediate 9 steps
rust
pub fn normalize_path(input: &str) -> String {
    let is_absolute = input.starts_with('/');
    let has_trailing_slash = input.len() > 1 && input.ends_with('/');
    let mut stack: Vec<&str> = Vec::new();

Normalizing filesystem paths in Rust

string-processing stack path-manipulation
Intermediate 8 steps
typescript
interface UserAgentInfo {
  browser: { name: string; version: string };
  os: { name: string; version: string };
  device: 'mobile' | 'tablet' | 'desktop';

Parsing a user-agent string with ordered rules

regex parsing pattern-matching
Intermediate 9 steps
php
<?php
 
namespace App\FeatureFlags;
 

How a feature flag evaluator decides

feature-flags rollout hashing
Intermediate 8 steps
ruby
class KeyTransformer
  def self.camelize(data)
    new.camelize(data)
  end

Recursively camelizing nested Ruby data

recursion data-transformation pattern-matching
Intermediate 7 steps
ruby
require "csv"
 
class CsvExporter
  def initialize(records, columns: nil)

Turning records into CSV in Ruby

csv data-export serialization
Intermediate 7 steps
rust
use std::collections::HashMap;
 
fn decode_component(input: &str) -> String {
    let bytes = input.as_bytes();

Parsing a URL query string in Rust

url-encoding byte-parsing iterators
Intermediate 8 steps
rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileKind {
    Png,
    Jpeg,

Detecting file types by magic bytes in Rust

pattern-matching byte-slices lookup-table
Intermediate 7 steps
rust
use std::collections::VecDeque;
 
#[derive(Debug)]
pub struct Hunk {

Applying a diff hunk in Rust

enums error-handling pattern-matching
Intermediate 8 steps
rust
use std::convert::TryFrom;
 
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HttpStatus {

Converting HTTP codes with TryFrom in Rust

enums error-handling trait-implementation
Intermediate 8 steps
rust
use chrono::{DateTime, Duration, FixedOffset, Utc};
 
#[derive(Debug)]
pub struct EventWindow {

Parsing and measuring time windows in Rust

datetime error-handling pattern-matching
Intermediate 7 steps
ruby
require 'json'
require 'set'
 
class SensitiveScrubber

Recursively scrubbing secrets from JSON

recursion data-masking pattern-matching
Intermediate 7 steps