Skip to content

Instantly share code, notes, and snippets.

View sug0's full-sized avatar
💭
debugging

Tiago Carvalho sug0

💭
debugging
View GitHub Profile
@sug0
sug0 / static_arc.rs
Last active March 25, 2022 01:43
Arc types in Rust with a predefined number of instances.
use std::ptr::NonNull;
use std::ops::{Deref, Drop};
use std::mem::{MaybeUninit, ManuallyDrop};
use std::sync::atomic::{AtomicUsize, Ordering};
#[derive(Debug)]
pub struct StaticArc<T> {
inner: NonNull<StaticArcInner<T>>,
}
@sug0
sug0 / union.go
Last active March 14, 2024 07:10
Go 1.18 tagged union types!
// no, this is not that useful
package main
import (
"fmt"
"unsafe"
)
type Kind[T any] struct{}
@sug0
sug0 / Bitlang.hs
Last active January 18, 2022 20:57
1 bit images in Haskell
{-# LANGUAGE NumericUnderscores #-}
module Bitlang
( compile
, evaluate
, generate
, ImageMeta(..)
, ExprMeta(..)
, Depth(..)
) where
@sug0
sug0 / adts.go
Last active August 14, 2021 18:40
Painful algebraic data types in Go
package main
import (
"fmt"
"unsafe"
)
// size = 8 + 8 = 16
type TaggedUnion struct {
Kind uintptr
@sug0
sug0 / alloc.rs
Last active June 24, 2021 16:39
Simple Rust alloc bench
use std::time::SystemTime;
fn main() {
let state = measure(alloc);
let _clone = measure(|| clone(&state));
}
#[inline]
fn measure<T, F: Fn() -> T>(f: F) -> T {
let start = SystemTime::now();
@sug0
sug0 / evil.py
Created February 11, 2021 23:51
PEP pattern matching is useless
def match(value, clauses):
for m in clauses:
try:
exec(f'{m} = {value}')
return clauses[m](locals())
except (TypeError, ValueError):
continue
raise ValueError('non exhaustive patterns')
def main():
@sug0
sug0 / life.scm
Created January 4, 2021 18:39
Conway's game of life in chez scheme
(define-record generation (num cols rows board))
(define (board-at g x y)
(let
([board (generation-board g)]
[cols (generation-cols g)])
(fxvector-ref board (+ (* y cols) x))))
(define (board-set! g x y value)
(let
@sug0
sug0 / poly.rs
Created December 29, 2020 02:13
Check if a point lies in a polygon in linear time.
macro_rules! p {
($x:expr, $y:expr) => { Point { x: $x, y: $y } }
}
#[derive(Debug, Copy, Clone)]
struct Point {
x: f32,
y: f32,
}
@sug0
sug0 / oldtestament.sh
Created November 1, 2020 03:18
Download the Old Testament in Portuguese from biblia.pt
#!/bin/sh
BOOK_GEN=50
BOOK_EXO=40
BOOK_LEV=27
BOOK_NUM=36
BOOK_DEU=34
BOOK_JOS=24
BOOK_JUI=21
BOOK_RUT=4
@sug0
sug0 / parsec.py
Last active August 25, 2020 21:58
Python parsec
from functools import reduce
def last(iterator):
item = None
for x in iterator:
item = x
return item
def deplete(iterator):
for x in iterator: