Skip to content

Instantly share code, notes, and snippets.

fn into_iter(list: Option<Box<ListNode>>) -> LLIntoIter {
LLIntoIter(list)
}
struct LLIntoIter(Option<Box<ListNode>>);
impl Iterator for LLIntoIter {
type Item = Box<ListNode>;
fn next(&mut self) -> Option<Box<ListNode>> {
let mut node = self.0.take()?;
fn interleave<I>(its: Vec<I>) -> Interleave<I> {
Interleave { its: its.into_iter().collect() }
}
struct Interleave<I> {
its: std::collections::VecDeque<I>,
}
impl<I: Iterator> Iterator for Interleave<I> {
type Item = I::Item;
use std::collections::HashMap;
use std::hash;
struct DisjointSet<T> {
elements: HashMap<T, usize>,
parents: HashMap<usize, usize>,
}
impl<T: Eq + hash::Hash> DisjointSet<T> {
pub fn new() -> Self {
// https://stackoverflow.com/a/39647997/1163020
// probably unsuitable for production.
use std::hash;
#[derive(Debug, Copy, Clone, PartialOrd)]
pub struct NormalF64(f64);
impl NormalF64 {
pub fn new(n: f64) -> Option<Self> {
if n == 0.0 { return Some(Self(0.0)); }
n.is_normal().then(|| Self(n))
// Adapted from Python's
struct Permutations<T> {
pool: Vec<T>,
indices: Vec<usize>,
cycles: Vec<usize>,
r: usize,
once: bool,
}
impl<T: Copy> Iterator for Permutations<T> {
-- Tables which are logically "deleted" with a `deleted` column,
-- but still respect foreign key constraints.. of a sort.
CREATE SCHEMA test;
CREATE TABLE test.foo_full (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
deleted BOOL DEFAULT false,
UNIQUE (id, deleted),
@orez-
orez- / split_by.rs
Last active February 25, 2024 01:51
struct SplitBy<'a, T> {
slice: &'a [T],
value: T,
last_idx: usize,
}
impl<'a, T: PartialEq> Iterator for SplitBy<'a, T> {
type Item = &'a [T];
fn next(&mut self) -> Option<Self::Item> {
@orez-
orez- / bigint.rs
Created June 23, 2021 00:20
an itermut implementation for a BigInt struct
const ZERO: u8 = '0' as u8;
#[derive(Debug)]
pub struct BigInt(Vec<u8>);
impl BigInt {
pub fn new(mut num: u64) -> Self { // not a good constructor, just for testing
let mut digits = Vec::new();
while num != 0 {
digits.push((num % 10) as u8);
num /= 10;
fn partition<'a>(s: &'a str, p: &str) -> (&'a str, &'a str) {
match s.find(p) {
Some(i) => (&s[..i], &s[i+p.len()..]),
None => (s, ""),
}
}
fn rpartition<'a>(s: &'a str, p: &str) -> (&'a str, &'a str) {
match s.rfind(p) {
Some(i) => (&s[..i], &s[i+p.len()..]),
def def def def ‐ def, def unless def
return def ‐ def unless def unless def > def unless def - def unless def
return def def def ‐ def, def unless def - def ‐ def unless def ‐ def > def unless def
return def def def ‐ def - def unless def, def unless def
end
# def def 400, 1280
# => 80
# gist mangles this. base64 of the original: