Skip to content

Instantly share code, notes, and snippets.

View sebinsua's full-sized avatar
🐚

Seb Insua sebinsua

🐚
View GitHub Profile
@sebinsua
sebinsua / crossorigin.plugin.ts
Created September 18, 2024 09:07
crossorigin.plugin.ts
import type { Plugin, UserConfig } from 'vite';
type CrossOriginValue = 'anonymous' | 'use-credentials' | '';
interface CrossOriginPluginOptions {
/**
* The value to set for the crossorigin attribute.
* @default 'use-credentials'
*/
crossOriginValue?: CrossOriginValue;
@sebinsua
sebinsua / smooth-with-array.ts
Last active March 11, 2024 12:02
Smooth a stream of LLM tokens into a stream of characters while reducing jitter by stabilising output timing. Explorations of different approaches.
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
class AsyncQueue<T> {
queuedItems: (T | Error)[];
queuedProcessors: [(item: T) => void, (error: Error) => void][];
constructor() {
// Note: The FIFO `shift` operations we do on these arrays are `O(n)`.
@sebinsua
sebinsua / follow.py
Last active August 23, 2023 21:37
An implementation of a sequential generator called `follow` for traversing linked lists and other data structures in a fixed manner.
from operator import eq, attrgetter as attr
from typing import TypeVar, Callable, Iterator, Optional
eq_or_none = lambda x, y: y is None or eq(x, y)
T = TypeVar('T')
def follow(start: T, next_fn: Callable[[T], T], stop: Callable[[T, T], bool] = eq_or_none) -> Iterator[Optional[T]]:
yield start
type Slice = [lowIndex: number, highIndex: number];
function getRandomIntegerInRange([low, high]: Slice): number {
return Math.floor(Math.random() * (high - low + 1)) + low;
}
function createPartitionForSlice([lowIndex, highIndex]: Slice) {
return function partition(pivotIndex: number, items: number[]): number {
// Partitioning a slice works like so:
// - We get the pivot value from the pivot index (in our case, a random index
@sebinsua
sebinsua / interesting-trie.ts
Last active August 9, 2023 16:27
I experimented a little bit with tries.
export class TrieNode {
public children: Record<string, TrieNode> = {};
public isEndOfWord = false;
}
export class Trie {
private root: TrieNode;
constructor() {
this.root = new TrieNode();
class Solution:
def search(self, nums: List[int], target: int) -> int:
# Special case for lists with only one or two elements.
if len(nums) <= 2:
try:
return nums.index(target)
except:
return -1
# As the array has been sorted and then rotated, it's not actually
class Solution:
def findMin(self, nums: List[int]) -> int:
# Special case for lists with only two elements.
if len(nums) == 2:
return min(*nums)
# As the array has been sorted and then rotated, it's not actually correctly
# sorted and we can't use a binary search in the conventional way. However,
# because we're able to make definitive decisions on which side the minimum
# value will be found at on every iteration we're still able to use binary
type Zipped<A extends readonly (readonly any[])[]> = {
[RowIndex in keyof A[0]]: RowIndex extends "length" ? number : {
[ArrayIndex in keyof A]: A[ArrayIndex] extends readonly any[] ? A[ArrayIndex][RowIndex] : never;
};
};
type NumericRange<TotalLength extends number, TempRange extends any[] = []> =
TempRange['length'] extends TotalLength ? TempRange : NumericRange<TotalLength, [...TempRange, TempRange['length']]>;
type TupleFromKeys<T, K extends readonly (keyof T)[]> = {
@sebinsua
sebinsua / default-object.ts
Last active July 21, 2023 14:40
WIP after playing around in GPT.
type Constructor<T> = new (...args: any[]) => T;
type DefaultType<T> = T extends Constructor<infer U> ? U : T;
function defaultObject<V, K extends string | symbol = string | symbol>(Type: Constructor<V> | V): Record<K, DefaultType<V>> & Iterable<[K, DefaultType<V>]> {
const store = new Map<K, DefaultType<V>>();
const iterable = {
*[Symbol.iterator] () {
for (let [key, value] of store) {
yield [key, value];
from typing import List, Tuple
from collections import defaultdict, deque
def next_moves(stones: List[int], stone: int, previous_jump: int) -> List[Tuple[int, int]]:
return [
(stone + next_jump, next_jump)
for next_jump in [previous_jump-1, previous_jump, previous_jump+1]
if next_jump > 0 and stone + next_jump in stones
]