Skip to content

Instantly share code, notes, and snippets.

template <typename T>
struct point { unsigned int left; unsigned int top; };
struct handle {
void *h;
};
struct window_handle: handle {};
struct screen_handle: handle {};
import Cocoa
import Dispatch
typealias Scalar = Float
let fullCircle = Scalar.pi * 2
let exponent = Int(CommandLine.arguments.dropFirst().first ?? "") ?? 5
let fractions = stride(from: Scalar(0.0),
data Tree a
= Empty
| Node a
(Tree a)
(Tree a)
rightmost :: Tree a -> [a]
rightmost = foldTree [] (\x l r -> x : zipPrefer r l)
foldTree :: b -> (a -> b -> b -> b) -> Tree a -> b
@progalgo
progalgo / RandomStream.cs
Created November 24, 2021 09:35
.NET Stream, that gives random bytes. Not cryptographically secure.
using System;
using System.IO;
class RandomStream : Stream
{
private readonly Random rng;
private long position = 0;
private readonly object advanceLock = new object();
@progalgo
progalgo / Minkowski.swift
Last active March 19, 2021 21:34
Simulation of travelling in Minkowski space
import Cocoa
typealias Scalar = Float80
let speedOfLight: Scalar = 299792458
let c = speedOfLight
struct MVector {
let time: Scalar
let x: Scalar
@progalgo
progalgo / frog_problem.py
Created October 16, 2020 08:19
Brute-force solution to statistical problem.
from math import sin, cos, pi, sqrt
from itertools import count, product, takewhile
from multiprocessing import Pool
from functools import lru_cache
class Vector:
"""Vector arithmetic"""
def __init__(self, iterable):
self.elements = tuple(iterable)
@progalgo
progalgo / parens.hs
Last active October 16, 2020 08:57
Output all correct parenthesies sequences for selected string length
suffixes len fval
| len < 0 = []
| fval < 0 = []
| len == fval = [replicate len ')']
| len < fval = []
| len > fval = map ("(" ++) (suffix (len-1) (fval+1))
++
map (")" ++) (suffix (len-1) (fval-1))
main :: IO()
@progalgo
progalgo / intervalGrouping.hs
Last active December 23, 2020 20:16
Get intervals of consecutive integers
import Data.List
import Data.List.GroupBy
data Group a = Single a | Interval a a
instance (Show a) => Show (Group a) where
show (Single i) = show i
show (Interval a b) = show a ++ "-" ++ show b
splitDisjoint = groupBy (\x y -> y == (x+1))