Skip to content

Instantly share code, notes, and snippets.

View eczn's full-sized avatar
Star

eczn* eczn

Star
View GitHub Profile
@eczn
eczn / units.tsx
Last active September 12, 2023 04:53
bytes storage units fomatter
export enum Units {
B = 1,
KB = 1 * 1024,
MB = 1 * 1024 * 1024,
GB = 1 * 1024 * 1024 * 1024,
TB = 1 * 1024 * 1024 * 1024 * 1024,
}
const UnitsName = ['B', 'KB', 'MB', 'GB', 'TB'];
@eczn
eczn / generic-round.tsx
Last active August 12, 2023 16:56
implementation of and increment for any number's roundding. the Math.round(x) is equivalent to genericRound(x, 1)
/**
* 实现任意 increment 的任意数字的 rounding 操作;
* 比如 Math.round(n) 其实就是 genericRound(n, 1) 的特化
*/
export function genericRound(n: number, inc: number): number {
if (inc === 0) return n;
const d = (n % inc);
if (d >= (inc / 2)) return n + (inc - d);
return n - d;
}
@eczn
eczn / list->tree.ss
Created August 11, 2023 15:16
由 list 出发去构造一颗平衡二叉树,scheme 实现,非常优雅
(define (list->tree elements)
(car (partial-tree elements (length elements))))
(define (partial-tree elts n)
(if (= n 0)
(cons '() elts)
(let ((left-size (quotient (- n 1) 2)))
(let ((left-result (partial-tree elts left-size)))
(let ((left-tree (car left-result))
(non-left-elts (cdr left-result))
@eczn
eczn / resolve-vscode-win7-extension-xhr-failed
Created July 18, 2023 09:55
Resolve VSCode Win7 Extension XHR Failed
# Resolve VSCode Win7 Extension XHR Failed
run vscode with command line flag `--ignore-certificate-errors`
@eczn
eczn / 01-withTimeoutContinuation.swift
Last active July 17, 2023 04:29
withTimeoutContinuation.swift
import Foundation
fileprivate var counter = 0
func withTimeoutUnsafeContinuation<T>(
_ labelName: String = "NoName",
_ timeout: TimeInterval = 2.0,
_ fn: @escaping (UnsafeContinuation<T?, Never>) -> Void
) async -> T? {
let nth = counter + 1
@eczn
eczn / workers.js
Created July 12, 2023 12:23 — forked from Papersman/workers.js
mikan_cloudflare_workers
/*
* https://github.com/netnr/workers
*
* 2019-10-12 - 2022-05-05
* netnr
*
* https://github.com/Rongronggg9/rsstt-img-relay
*
* 2021-09-13 - 2022-05-29
* modified by Rongronggg9
@eczn
eczn / tree.ts
Last active August 6, 2023 10:24
functional data structure ?
// 从三个参数中选择一个
type Selector<T> =
(v: T, l?: Tree<T>, r?: Tree<T>) => undefined | T | Tree<T>;
// 定义树
type Tree<T> = (
<S extends Selector<T>>(selector: S) => ReturnType<S>
);
// 构造 Tree
@eczn
eczn / noise.tsx
Last active April 4, 2023 02:58
白噪音生成 & 渲染到 canvas 上 (备注:gpt 教我写的,我改了其中相关着色器的参数以及简化和理解)
// export {}
const canvas = document.getElementById('canvas')! as HTMLCanvasElement;
const gl = canvas.getContext('webgl')!;
// 定义顶点着色器
const vertexShaderSource = `
attribute vec4 a_position;
void main() {
gl_Position = a_position;
@eczn
eczn / create-async-worker.ts
Created February 22, 2023 08:59
异步并发控制,曾经某郭姓男子问的【存档备份】
type Task<T> = () => (T | Promise<T>);
type UnLock = () => void;
class ProcessLock {
private count = 0;
private queue: UnLock[] = [];
constructor(public capacity: number) {}
request = async (): Promise<UnLock> => {
if (this.count >= this.capacity) {
await new Promise<void>((resolve) => {
@eczn
eczn / output.sh
Last active January 9, 2023 03:06
output stdout & stderr to one file
echo 'hello' > ./a.log 2>&1 # merge stdout & stderr and redirect it to a.log
echo "$?" # outout the process exitCode of last executed command (L1's exitCode in this case)