Code Explainers

Code explainers tagged #iterators

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
php
<?php
 
namespace App\Support;
 

Recursively finding files with SPL iterators in PHP

recursion iterators filesystem
Intermediate 7 steps
rust
use axum::{
    extract::FromRequestParts,
    http::{request::Parts, StatusCode, header::ACCEPT_LANGUAGE},
};

A locale extractor for Axum handlers

content-negotiation http-headers custom-extractor
Intermediate 7 steps
rust
pub struct Fibonacci {
    current: u64,
    next: u64,
}

A Fibonacci iterator in Rust

iterators traits lazy-evaluation
Intermediate 6 steps
rust
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::Path;

Parsing INI files in Rust

parsing file-io hashmap
Intermediate 9 steps
typescript
function* map<T, U>(source: Iterable<T>, fn: (value: T, index: number) => U): Generator<U> {
  let index = 0;
  for (const value of source) {
    yield fn(value, index++);

Building a lazy iterator pipeline in TypeScript

generators lazy-evaluation iterators
Intermediate 7 steps
rust
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
 
#[derive(Debug)]

Parsing ps output into structs in Rust

subprocess parsing iterators
Intermediate 8 steps
rust
use std::collections::HashMap;
 
#[derive(Debug, Clone)]
struct Row {

Building a tree from flat parent-id rows in Rust

recursion hashmap tree-building
Intermediate 6 steps
php
<?php
 
declare(strict_types=1);
 

An immutable typed collection in PHP

immutability value-objects iterators
Intermediate 8 steps
python
import hashlib
from pathlib import Path
 
 

Streaming SHA-256 checksums in Python

hashing file-io streaming
Intermediate 6 steps
rust
use std::num::ParseIntError;
 
fn parse_line(line: &str) -> Result<Vec<i64>, ParseIntError> {
    line.split(',')

Collecting Results in Rust iterators

iterators error-handling result
Intermediate 7 steps
rust
use std::collections::HashMap;
 
#[derive(Debug, Clone)]
struct Record {

Batched aggregation with fold in Rust

iterators fold hashmap
Intermediate 9 steps
rust
use std::collections::HashMap;
 
pub fn word_frequencies(text: &str) -> HashMap<String, usize> {
    let mut counts: HashMap<String, usize> = HashMap::new();

Counting and ranking words in Rust

hashmap iterators sorting
Intermediate 7 steps
rust
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::Path;
 

Buffered log scanning in Rust

buffered-io error-handling streaming
Intermediate 9 steps
javascript
class Stack {
  #items = [];
 
  push(value) {

Building a Stack with private fields

data-structures encapsulation iterators
Intermediate 7 steps
rust
/// A custom iterator that yields Fibonacci numbers up to a maximum value.
pub struct Fibonacci {
    current: u64,
    next: u64,

Building a custom Fibonacci iterator in Rust

iterators traits state-machine
Intermediate 6 steps
ruby
class LinkedList
  include Enumerable
 
  Node = Struct.new(:value, :next_node)

Building an enumerable linked list in Ruby

linked-list enumerable data-structures
Intermediate 7 steps