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 / church.go
Last active March 31, 2023 17:06
Church numerals in Go
// next up: scott encoding https://en.wikipedia.org/wiki/Mogensen%E2%80%93Scott_encoding
package main
import "fmt"
type Church[T any] func(func(T) T) func(T) T
func main() {
churchTest[int](0, func(x int) int { return x + 1 })
@sug0
sug0 / trie.rs
Last active December 10, 2022 00:59
Rust code generator to match against a set of N pre-defined strings, in logarithmic time
use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::io::{self, BufRead};
#[derive(Debug, Default)]
struct Node {
terminal: bool,
children: BTreeMap<char, Node>,
}
@sug0
sug0 / xbps.c
Last active October 24, 2022 22:53
Small utility program to spin up xbps sub-commands.
// Copyright (C) 2022 Tiago Carvalho <[email protected]>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
@sug0
sug0 / merkle.rs
Created September 12, 2022 07:42
Simple Merkle tree in Rust
#![feature(iter_array_chunks)]
#![feature(hasher_prefixfree_extras)]
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[derive(Clone, Debug)]
struct Tree<T> {
root: Option<Node<T>>,
}
@sug0
sug0 / conts.py
Last active September 8, 2022 20:51
Promises-like API in Python
import multiprocessing
from collections import namedtuple
from concurrent.futures import ThreadPoolExecutor
Poll = namedtuple('Poll', ['resolved', 'value'])
_CURRENT_EXECUTOR = None
def enter_runtime(main=None, threads=0):
@sug0
sug0 / parsers.scm
Last active May 30, 2022 18:55
Parser combinators in Chez Scheme
;; TODO: add backtracking point, to avoid parsing all the way from the beginning!
;;
;; e.g.
;;
;; (json-value (new-string-state "[\" \" 123]"))
;; ==> (#f "Exhausted all possible JSON value types: Failed to parse JSON number: Expected character '9', but found '[' instead")
;;
;; we know we are looking for a comma, so add a backtracking point after the string
;; return 1 >>= thing1 >>= thing2 >>= thing3
@sug0
sug0 / specialization.rs
Last active April 24, 2022 21:05
Specialization idiom in stable Rust
use std::fmt::Debug;
use std::marker::PhantomData;
enum Specialized {}
enum Generic {}
struct Thing<T, K = Generic> {
inner: T,
_phantom: PhantomData<K>,
@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