Skip to content

Instantly share code, notes, and snippets.

@sno2
sno2 / fast-string-type-length.ts
Created August 15, 2022 21:54
A fast string length implementation in the TypeScript type system
/**
* A logarithmic string length algorithm in the TypeScript type system utilizing
* a memoizing recursive type. Computes the length of any string with far
* superior performance than any other current implementations via a doubling
* cache and string patterns backing the checks. Works for any string with less
* than 10,000 characters due to the tuple size limits for the cache (in total).
*
* @author Carter Snook <cartersnook04@gmail.com> github.com/sno2
* @license https://unlicense.org/ (credit would be nice tho)
*/
@sno2
sno2 / deno-rust-bert-qa-loop.txt
Last active November 17, 2021 17:18
two machines talking to each other
A: How are you?
B: Good. How is your day going?
A: Tentatively good! I'm doing a bit of homework that's due tomorrow but it doesn't mean much! I've got some more projects to finish today though so that's good
B: What's your most favourite thing you've done for a project?
A: I really liked writing for a bit. Writing for a company I've worked for was a bit tough.
B: I like writing too! What did you write?
A: A story that I was working on at the time I wrote this one. I've also been writing a lot lately, actually.
@sno2
sno2 / goify-promise-examples.ts
Last active June 10, 2021 10:02
Turns any function that returns a promise into a function that returns the promise with the value and error without throwing.
function add(a: number, b: number): Promise<number> {
return Promise.resolve(a + b);
}
{
const safeAdd = goify(add);
const [val /* 7 */, err /* null */] = await safeAdd(2, 5);
}
import { AES } from "https://deno.land/x/god_crypto/aes.ts";
import { encode, decode } from "https://deno.land/std@0.97.0/encoding/hex.ts";
class SecretStore {
#aes: AES;
constructor(data: { key: string; iv?: string }) {
this.#aes = new AES(data.key, {
mode: "cbc",
iv: data.iv ?? "random 16byte iv",
/** Makes a tuple of length `L` with each of the elements of type `T`. */
export type Locked<
L extends number,
T,
$Draft extends unknown[] | readonly unknown[] = []
> =
// jus making my ternaries look nicer in prettier
$Draft["length"] extends L
? $Draft // ship it
: $Draft["length"] extends 44 // it will overflow if it's larger than 44 so we need to do the hacky way
/* Sass Code */
@mixin vendor-prefix($name, $value) { /* Copy this mixin for your own use! */
@each $vendor in ('-webkit-', '-moz-', '-ms-', '-o-', '') {
#{$vendor}#{$name}: #{$value};
}
}
* {
@include vendor-prefix('box-sizing', 'border-box');
}
/* Sass Code */
@import 'style_reset'; /* File Path must be relative | the style_reset.scss file is in the variables folder */
html {
background: #fff; /* $blue was pulled in from the file above */
}
/* Compiled CSS Code */
html, body, ul, ol { /* The style_reset.scss file contents */
margin: 0;
/* Sass Code */
$headerHeight: 15em;
header {
width: 100%;
height: $headerHeight;
padding: $headerHeight/4 5em;
}
/* Compiled CSS Code */
/* Sass Code */
@mixin phone {
@media screen and (min-width: 480px) {
@content;
}
}
@mixin tablet {
@media screen and (min-width: 768px) {
@content;
@sno2
sno2 / Nesting.css
Last active January 22, 2020 03:57
/* Sass Code */
html {
width: 100%;
body {
width: 50%
/* This would only be styled if body was in html */
}
}