Skip to content

Instantly share code, notes, and snippets.

View CMCDragonkai's full-sized avatar
🚀
Lightspeed

Roger Qiu CMCDragonkai

🚀
Lightspeed
View GitHub Profile
@CMCDragonkai
CMCDragonkai / range.ts
Last active October 10, 2021 09:23
Generators and Iterators in JavaScript #javascript #typescript
function* range(start: number, stop?: number, step = 1): Generator<number> {
if (stop == null) {
stop = start;
start = 0;
}
for (let i = start; step > 0 ? i < stop : i > stop; i += step) {
yield i;
}
}
@CMCDragonkai
CMCDragonkai / object_map.ts
Created October 5, 2021 03:58
Singleton Object Map using Locking #typescript #javascript
import { Mutex, MutexInterface } from 'async-mutex';
type ObjectId = string;
type Object = number;
type ObjectMap = Map<ObjectId, {
object?: Object;
lock: MutexInterface;
}>;
@CMCDragonkai
CMCDragonkai / rwlock-read.ts
Last active June 22, 2022 02:14
Read Write Lock #javascript #typescript #concurrency
import type { MutexInterface } from 'async-mutex';
import { Mutex } from 'async-mutex';
/**
* Single threaded read-preferring read write lock
*/
class RWLock {
protected _readerCount: number = 0;
protected _writerCount: number = 0;
protected lock: Mutex = new Mutex();
@CMCDragonkai
CMCDragonkai / concurrent_coalesced_async_construction.ts
Created September 28, 2021 03:45
Concurrently Coalesced Asynchronous Construction #typescript
import { Mutex, MutexInterface } from 'async-mutex';
type ObjectId = string;
type Object = number;
type ObjectMap = Map<ObjectId, {
object?: Object;
lock: MutexInterface;
}>;
@CMCDragonkai
CMCDragonkai / hybrid_methods.ts
Last active October 19, 2021 06:06
Hybrid Promise and Callback Methods #javascript #typescript
import callbackify from 'util-callbackify';
type Callback<P extends Array<any> = [], R = any, E extends Error = Error> = {
(e: E, ...params: Partial<P>): R;
(e?: null | undefined, ...params: P): R;
};
async function maybeCallback<T>(
f: () => Promise<T>,
callback?: Callback<[T]>
@CMCDragonkai
CMCDragonkai / 1_async_create_destroy.ts
Last active September 30, 2021 07:41
Asynchronous Initialisation and Deinitialisation for JavaScript Classes #typescript #javascript
/**
* Use this when object lifetime matches object "readiness"
*/
class X {
protected _destroyed: boolean = false;
public static async createX(): Promise<X> {
return new X;
}
@CMCDragonkai
CMCDragonkai / rec_params.ts
Last active July 27, 2021 05:48
Accumulating Parameter Pattern in JavaScript #javascript #typescript
// mutating input and output
const rec = (inputs: Array<number>, outputs: Array<number> = []) => {
if (!inputs.length) {
return outputs;
} else {
const input = inputs.shift()!;
outputs.push(input + 1);
return rec(inputs, outputs);
}
};
@CMCDragonkai
CMCDragonkai / aws_authenticate_docker_ecr.sh
Created July 1, 2021 03:37
AWS Authenticate Docker to ECR #aws #ecr #docker
#!/usr/bin/sh
aws ecr get-login-password --region ap-southeast-2 | docker login --username AWS --password-stdin <ENTERID>.dkr.ecr.ap-southeast-2.amazonaws.com
@CMCDragonkai
CMCDragonkai / cifar10_loading.py
Last active June 16, 2021 03:49
CIFAR10 Loading #python
import os
import pickle
import numpy as np
from pathlib import Path
from typing import List, Tuple
# expect CIFAR10 to point to a directory like
# .
# ├── batches.meta
# ├── data_batch_1
@CMCDragonkai
CMCDragonkai / lockpidfile.py
Last active January 19, 2022 15:20
Singleton Process with PID Lock File #python
import os
import fcntl
import contextlib
import time
@contextlib.contextmanager
def lockpidfile(filepath):
with os.fdopen(
os.open(filepath, os.O_RDWR | os.O_CREAT, mode=0o666),