Skip to content

Instantly share code, notes, and snippets.

@jwulf
jwulf / naive-ratio-rate-limiter.ts
Created December 1, 2020 03:05
A naive functional rate limiter that prevents queue starvation
interface QueuedTask<T> {
task: () => T;
promise: {
resolve: (res: any) => void;
reject: (err: any) => void;
};
}
interface RateLimitedTask<T> {
task: () => T;
@jwulf
jwulf / naive-rate-limiter.ts
Created December 1, 2020 01:03
A naive functional rate limiter with an absolute limit
interface QueuedTask<T> {
task: () => T;
promise: {
resolve: (res: any) => void;
reject: (err: any) => void;
};
}
interface RateLimitedTask<T> {
task: () => T;
// Don't ask about this one. I have no idea what this magic is.
// Copied from https://github.com/microsoft/TypeScript/issues/13298#issuecomment-724542300.
type UnionToIntersection<U> = (
U extends any ? (arg: U) => any : never
) extends (arg: infer I) => void
? I
: never;
// Continuation of above.
type UnionToTuple<T> = UnionToIntersection<(T extends any ? (t: T) => T : never)> extends (_: any) => infer W
@sogaiu
sogaiu / lpeg-in-fennel.fnl
Last active June 8, 2021 18:05
lpeg in fennel
>> (local lpeg (require :lpeg))
nil
>> (local equalcount (lpeg.P {1 "S"
.. "S" (+ (* "a" (lpeg.V "B")) (* "b" (lpeg.V "A")) "")
.. "A" (+ (* "a" (lpeg.V "S")) (* "b" (lpeg.V "A") (lpeg.V "A")))
.. "B" (+ (* "b" (lpeg.V "S")) (* "a" (lpeg.V "B") (lpeg.V "B")))}))
nil
>> (lpeg.match equalcount "aabb")
5
>> (lpeg.match equalcount "baabba")
@plembo
plembo / honeyvp8koniotvlan.md
Last active August 17, 2024 15:18
Honeywell VisionPro 8000 on an IoT VLAN

Honeywell VisionPro 8000 on an IoT VLAN

Honeywell wifi thermostats have a bit of a reputation when it comes to playing nice with even the simplest home network. It only gets worse on slightly more sophisticated environments. But I wanted to put all my IOT devices on their own VLAN for security, so it was time to face the angry bear.

Note that many hours were wasted due to my own ignorance, and a remarkable lack of practical examples in product documentation. Sometimes I thought that the manufacturers really didn't want anyone to succeed at this. Having said that, it's clear from the many responses to questions in customer forums that isn't actually the case. It's just the operation of the old adage, "Those who can, can't teach".

@cleoold
cleoold / bitstring.ts
Created November 9, 2020 04:34
bitstring manip in typescript type system
/**
* Binary string and bitwise arithmetic entirely in TypeScript's type system
* (with template string types).
*
* Requires version >= 4.1.0
*
* Hover on the types to see the results.
*/
type A = '010101'
#Requires -Version 6
# Version 1.2.4
# check if newer version
$gistUrl = "https://api.github.com/gists/198cb33afea7585d915abc08f6a849e7"
$latestVersionFile = Join-Path -Path ~ -ChildPath ".latest_profile_version"
$versionRegEx = "# Version (?<version>\d+\.\d+\.\d+)"
if (Test-Path $latestVersionFile) {
@joaomilho
joaomilho / log.ts
Last active September 17, 2022 06:41
A printf with dependant types in TypeScript, similar to Idris (https://gist.github.com/chrisdone/672efcd784528b7d0b7e17ad9c115292)
type FieldType<Field> =
's' extends Field ? string :
'f' extends Field ? number :
'i' extends Field ? number :
'd' extends Field ? number :
'o' extends Field ? HTMLElement :
'O' extends Field ? object :
'c' extends Field ? string :
never;
@benkehoe
benkehoe / py-args-for-bash.sh
Last active September 4, 2024 20:29
Python argument parsing for bash scripts
#!/bin/sh
# MIT No Attribution
#
# Copyright 2020 Ben Kehoe
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
@shicks
shicks / num.ts
Last active October 16, 2020 10:14
Parsing integers in TypeScript's type system
type NumCat<X, Y> = X extends any[] ? Y extends any[] ? [...X, ...X, ...X, ...X, ...X, ...X, ...X, ...X, ...X, ...X, ...Y] : never : never;
type OneDigit = {
'0': [], '1': [any], '2': [any, any], '3': [any, any, any],
'4': [any, any, any, any], '5': [any, any, any, any, any],
'6': [any, any, any, any, any, any], '7': [any, any, any, any, any, any, any],
'8': [any, any, any, any, any, any, any, any],
'9': [any, any, any, any, any, any, any, any, any],
};
type Digit<S> = S extends keyof OneDigit ? OneDigit[S] : never;
type Len<X> = X extends any[] ? X['length'] : never;