Skip to content

Instantly share code, notes, and snippets.

View shovon's full-sized avatar
💻
Working on my own projects

Sal Rahman shovon

💻
Working on my own projects
View GitHub Profile
@shovon
shovon / traversal.ts
Last active November 17, 2022 19:11
Determine if an adjacency list represents a single graph
interface ReadOnlyMapNoGetter<K, V> {
has(key: K): boolean;
entries(): IterableIterator<[K, V]>;
forEach(cb: (value: V, key: K, map: ReadOnlyMapNoGetter<K, V>) => void): void;
keys(): IterableIterator<K>;
values(): IterableIterator<V>;
readonly size: number;
}
interface ReadOnlyMap<K, V> extends ReadOnlyMapNoGetter<K, V> {
class Trie {
private children: Map<string, Trie> = new Map();
insert(value: string) {
if (value === "") {
return;
}
const first = value[0];
if (!first) {
throw new Error("Expected the first element to not be an empty string.");
@shovon
shovon / cons-list.ts
Created March 29, 2022 01:47
Lisp-style cons-based list
type List<T1> = [T1, List<T1> | null];
function* iterateCons<T>([left, right]: List<T>): IterableIterator<T> {
if (right) {
yield* iterateCons(right);
}
yield left;
}
const cons = <T1, T2>(left: T1, right: T2): [T1, T2] => [left, right];
type Pipe<T> = {
_<V>(fn: (value: T) => V): Pipe<V>;
readonly value: T;
};
function start<T>(initial: T): Pipe<T> {
return {
_<V>(fn: (value: T) => V): Pipe<V> {
return start(fn(initial));
},
export type Listener<T> = (value: T) => void;
export type OperatorFunction<T, V> = (
oberver: IObservable<T>
) => IObservable<V>;
export interface IObservable<T> {
subscribe(listner: Listener<T>): () => void;
}
@shovon
shovon / ExpandingInput.tsx
Last active March 28, 2022 03:50
An input element that expands as you type into it
import { useEffect, useRef, useState } from "react";
export default function ExpandableInput(
props: React.DetailedHTMLProps<
React.InputHTMLAttributes<HTMLInputElement>,
HTMLInputElement
>
) {
const [value, setValue] = useState(props.value);
const inputRef = useRef<HTMLInputElement | null>(null);
export type Links = { [key: string]: Link };
export type StatusCode =
| "400"
| "401"
| "402"
| "403"
| "404"
| "405"
| "406"
@shovon
shovon / README.md
Last active February 23, 2021 15:21

console.debug debugger helper

A simple module for helping you write debugger outputs. Makes debug outputs look neat and organized.

Usage

Copy and paste the index.ts file to some debug.ts file.

import createDebugger from './debug';
@shovon
shovon / README.md
Last active December 7, 2020 21:51
Implementation of writing a WAV file.

Usage

go run main.go
# Result should be in example.wav.
#
# Listen to the result using whatever audio player that supports WAV
@shovon
shovon / webpack.config.js
Created September 22, 2020 15:10
Webpack config with web workers directly embedded in the JavaScript
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
libraryTarget: "commonjs",
},
devtool: "source-map",