Skip to content

Instantly share code, notes, and snippets.

View eczn's full-sized avatar
Star

eczn* eczn

Star
View GitHub Profile
@eczn
eczn / lexer.mbt
Last active May 3, 2024 04:32
parser combinator 词法解析器
enum Token {
Value(Int)
LParen;
RParen;
Plus;
Minus;
Multiply;
Divide;
} derive(Debug)
; ___ _ __ ___ __ ___
; / __|_ _ __ _| |_____ / /| __|/ \_ )
; \__ \ ' \/ _` | / / -_) _ \__ \ () / /
; |___/_||_\__,_|_\_\___\___/___/\__/___|
; An annotated version of the snake example from Nick Morgan's 6502 assembly tutorial
; on http://skilldrick.github.io/easy6502/ that I created as an exercise for myself
; to learn a little bit about assembly. I **think** I understood everything, but I may
; also be completely wrong :-)
@eczn
eczn / scale-canvas.ts
Created January 19, 2024 09:59 — forked from callumlocke/scale-canvas.ts
How to fix a canvas so it will look good on retina/high-DPI screens.
/*
UPDATED for 2023 - Now much simpler. The old tricks are no longer needed.
The following code makes an 800×600 canvas that is always as sharp as possible for the device.
You still draw on it as if it's the logical size (800×600 in this case), but everything just
looks sharper on high-DPI screens. Regular non-sharp screens are not affected.
*/
const width = 800
@eczn
eczn / createSignal.tsx
Created January 5, 2024 07:04
createSignal for react
import React from 'react';
/** createSignal for react */
export function createSignal<S>(
initialState: S
): [() => S, (nextState: S) => void] {
const forceRender = useForceRender();
const stateRef = React.useRef<S>(initialState);
const stateRefGetter = (): S => stateRef.current;
@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