Skip to content

Instantly share code, notes, and snippets.

View Phryxia's full-sized avatar

Phryxia Phryxia

  • South Korea
View GitHub Profile
@Phryxia
Phryxia / median.ts
Last active July 25, 2022 07:01
TypeScript implementation of median value finder
function medianBySort(values: number[]) {
values.sort((a, b) => a - b)
const half = Math.floor(values.length / 2)
if (values.length % 2) {
return values[half]
}
return (values[half - 1] + values[half]) * 0.5
}
function medianByK(values: number[], k: number): number {
@Phryxia
Phryxia / rmq.ts
Last active June 19, 2022 10:49
TypeScript implementation of Range Minimum Query (RMQ) using Sparse Table
function getP(x: number): number {
return Math.floor(Math.log2(x))
}
export interface ResultPair<T> {
value: T
index: number
}
export function rmq<T>(
@Phryxia
Phryxia / bezier.ts
Last active December 27, 2024 07:49
TypeScript implementation of bezier curve and its derivative.
type Vector = number[]
function add(u: Vector, v: Vector): Vector {
return u.map((x, i) => x + v[i])
}
function sub(u: Vector, v: Vector): Vector {
return u.map((x, i) => x - v[i])
}
@Phryxia
Phryxia / regularExpressionRecognizer.ts
Last active May 23, 2022 03:22
Regular Language Description Language Recognizer
const CHARSET = '01'
const UNRECOGNIZED = -1
// -1: unrecognized
// other: next token position
type NextIndex = number | typeof UNRECOGNIZED
type Recognizer<T extends any[] = []> = (i: number, ...rest: T) => NextIndex
function parse(s: string): boolean {
const isRecognizedChar: Recognizer<[string]> = (i, allowedCharacters) => {
@Phryxia
Phryxia / pseudoGeneratorFunction.js
Last active May 18, 2022 16:25
What if there were no function* and yield syntax in the JavaScript?
/*
* Please read the comment first to understand this creepy pasta.
* Unfortunately, I don't have any ideas of implementing complete replica of function* (for return behavior)
*
* @param {function} f Schema of generator function scheme which receives any parameters and
* - may return `{ value, nextF }` or `undefined` if there is nothing to yield.
* - *value* is for `yield` and *nextF* for aftermath of `yield`.
* - *nextF* has the same manner as *f*.
* @return {function} Generator function which returns generator, which behaves like
* - as if returned values of f were 'yielded'.
@Phryxia
Phryxia / InteractiveFileInput.tsx
Created May 13, 2022 05:33
React component for interactive input for file which can determine to open dialogue or not.
import { DetailedHTMLProps, InputHTMLAttributes, MouseEvent, useRef } from 'react'
interface Props extends DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement> {
isApprovedInitial?: boolean
onBeforeFileSelection(): Promise<boolean>
}
export default function InteractiveFileInput({ isApprovedInitial, onBeforeFileSelection, ...rest }: Props) {
const dom = useRef<HTMLInputElement>(null)
const isApproved = useRef<boolean>(isApprovedInitial ?? false)
@Phryxia
Phryxia / fastIntegerExponent.ts
Last active April 16, 2022 15:52
TypeScript implementation of fast integer exponent calculation using divide and conquer
/**
* Return the exp-th power of base in O(lg exp) time complexity.
* @param {number} base - any real number
* @param {number} exp - any non negative integer
* @returns base ^ exp
*/
export function pow(base: number, exp: number): number {
if (exp <= 0) return 1
if (exp === 1) return base
@Phryxia
Phryxia / findDecisionBoundary.ts
Last active April 13, 2022 20:47
Find simple binary decision boundary of heaviside step function using binary search
export interface FinderOption {
f: (x: number) => boolean
p: number
epsilon: number
}
// This is theoretically pure, but impractical since this may result stack overflow due to heavy recursion
export function findDecisionBoundary(option: FinderOption, lower: number, upper: number, n: number = 0): {
boundary?: number
n: number
@Phryxia
Phryxia / useOutsideClickHandler.ts
Last active May 12, 2025 17:56
Simple react hook implementation for detecting click of outside of the given DOM
import { useCallback, useRef } from 'react'
type MouseEventHandler = (e: MouseEvent) => void
export function useOutsideClickHandler<T extends HTMLElement>(
callback: MouseEventHandler,
) {
const userCallback = useRef<MouseEventHandler>(() => {})
userCallback.current = callback
@Phryxia
Phryxia / findGreatestElement.ts
Last active March 27, 2022 10:03
TypeScript implementation of finding greatest element in partially ordered set.
export type Relation = [string, string]
function makeGraph(relations: Relation[]): Record<string, string[]> {
const result: Record<string, string[]> = {}
relations.forEach(([u, v]) => {
(result[u] ??= []).push(v)
result[v] ??= []
})
return result
}