Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View catnipan's full-sized avatar
🐈

Yifan Pan catnipan

🐈
View GitHub Profile
@catnipan
catnipan / useStateWithAction.ts
Last active February 8, 2024 21:21
state machine: add handler to state in a type safe way
type AnyState = { step: string };
type AnyActionMap = Record<string, (...args: any[]) => unknown>;
type AnyAction<State extends AnyState> = {
[S in State['step']]?: (
state: { step: S } & State,
setState: (newState: SetStateAction<State>) => void
) => AnyActionMap;
};
type StateWithActions<
@catnipan
catnipan / recurrence_macro.rs
Last active January 4, 2021 19:53
recurrence - a Rust macro example
// Credit to https://danielkeep.github.io/practical-intro-to-macros.html
macro_rules! count_exprs {
() => (0);
($head:expr $(, $tail:expr)*) => (1 + count_exprs!($($tail),*));
}
macro_rules! recurrence {
( $seq:ident [ $ind:ident ]: $sty:ty = $recur:expr, $($inits:expr),+) => {
{