This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()?; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()..]), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
NewerOlder