Skip to content

Instantly share code, notes, and snippets.

@kccqzy
kccqzy / unsure_calc.py
Created April 15, 2025 19:49
The unsure calculator inspired by https://filiph.github.io/unsure/
import numpy as np
import re
NUMBER = re.compile(r'[0-9]+(\.[0-9]+)?')
def tokenize(s):
orig_str = s
while len(s) > 0:
m = NUMBER.match(s)
if m:
@kccqzy
kccqzy / SimpleSub.hs
Created October 23, 2024 13:54
Quick implementation of SimpleSub (please see the paper "The Simple Essence of Algebraic Subtyping")
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Trans.Except
import Control.Monad.Trans.State
import Control.Monad.Trans.Writer.CPS
import Data.Foldable
import qualified Data.IntMap as IM
@kccqzy
kccqzy / AlgorithmW.hs
Created July 13, 2024 17:55
A toy implementation of Algorithm W for HN readers (modified from https://github.com/wh5a/Algorithm-W-Step-By-Step/blob/master/AlgorithmW.lhs)
import Control.Monad.Except
import Control.Monad.State
import qualified Data.IntMap as IntMap
import qualified Data.IntSet as IntSet
import qualified Data.Map as Map
data Exp
= EVar String
| ELit Lit
| EApp Exp Exp
@kccqzy
kccqzy / bus_schedule.cc
Last active October 9, 2022 00:16
The Bus Schedule Problem (Problem B)
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
@kccqzy
kccqzy / make_tree_for_range.py
Created April 5, 2021 19:32
Make a binary tree based on binary numbers
import sys
import itertools
def next_power_of_two(v):
# From Stanford bit-twiddling hacks: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
v -= 1
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
#include <algorithm>
#include <array>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
static const size_t STACK_SIZE = 16777216;
static const size_t DEFAULT_THREADS = 4;
struct Runtime;
@kccqzy
kccqzy / pext_utf8_decode.cpp
Created February 1, 2019 08:15
Test UTF-8 decoder using PEXT instruction
#include <stdlib.h>
unsigned pext_utf8_decode(unsigned char*& buf) {
unsigned next4;
__builtin_memcpy(&next4, buf, 4);
next4 = __builtin_bswap32(next4);
if (__builtin_expect(!!(next4 >> 31), 0)) {
// multi-byte handling
unsigned r;
if (((next4 >> 16) & 0b11100000'11000000) == 0b11000000'10000000) {
@kccqzy
kccqzy / cumulative1.hs
Last active July 6, 2018 23:31
Cumulative monoidal append
module Cumulative
( makeCumulative
, makeCumulativeReverse
) where
import Data.Traversable
import Data.Monoid
-- | Make a traversable data structure cumulative by performing a left-biased
-- partial sum (actually a monoidal append). Linear time.
@kccqzy
kccqzy / ieee754_explain.cpp
Created May 14, 2017 04:55
Explain an IEEE754 single-/double-precision floating point number
#include <bitset>
#include <iomanip>
#include <iostream>
#include <string.h>
namespace {
template <typename Floating, typename Integral, int expBits>
void explain(Floating x) {
static_assert(sizeof(Floating) == sizeof(Integral),
"types not the same size");
@kccqzy
kccqzy / FRM.hs
Last active April 26, 2016 17:13
Quick implementation of a segment tree with fast range mconcat
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NoMonoLocalBinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
module FRM
( FastRangeMconcat
, fromVector
, toVector
, (!), (!?)