Skip to content

Instantly share code, notes, and snippets.

View freddi301's full-sized avatar

Frederik Batuna freddi301

View GitHub Profile
@freddi301
freddi301 / useUndoReducer.ts
Last active October 26, 2019 17:10
Undoable Reducer react hooks #react #hook
function useUndoReducer<State, Action>(
reducer: (state: State, action: Action) => State,
initial: () => State[]
) {
const [index, setIndex] = useState(0);
const [history, setHistory] = useState(initial);
const state = history[index];
const dispatch = useCallback(
(action: Action) => {
const updated = history.slice(0, index + 1);
@freddi301
freddi301 / useUndoState.ts
Last active October 11, 2019 09:52
Unduoable state react hook #react #hook
function useUndoState<T>(initial: T) {
const [index, setIndex] = useState(0);
const [history, setHistory] = useState([initial]);
const state = history[index];
const setState = useCallback(
(update: (s: T) => T) => {
const updated = history.slice(0, index + 1);
updated.push(update(state));
setHistory(updated);
setIndex(index + 1);
@freddi301
freddi301 / fiber.ts
Created August 20, 2019 22:27
Fiber (react fibers outside react) immutable version
type FiberFunction<Args extends any[], Return> = (
cell: CellFunction
) => (...args: Args) => Return;
type FiberInstance<Args extends any[], Return> = (
...args: Args
) => [Return, FiberInstance<Args, Return>];
type FiberFactory = <Args extends any[], Return>(
fiber: FiberFunction<Args, Return>,
@freddi301
freddi301 / AsyncNode.tsx
Last active August 26, 2019 09:59
React component and hook for rendering promises
export function AsyncNode({ children }: { children: Promise<ReactNode> }) {
return useLatestResolvedValue<ReactNode>(children, () => null) as any;
// as any assertion needed for issue https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20356#issuecomment-336384210
}
export function useLatestResolvedValue<Value>(
promise: Promise<Value>,
initial: () => Value
) {
const [[value], dispatch] = useReducer<
@freddi301
freddi301 / lazy.ts
Last active October 25, 2019 13:49
Typescript call by need aka lazy
export type Lazy<Value> = () => Value
export function lazy<Value>(thunk: () => Value){
let value: Value // will hold computed value
let factory: (() => Value) | null = thunk // used as check if value is computed and allow thunk to be garbage collected
return () => {
if (!factory) {
return value
} else {
value = factory()
@freddi301
freddi301 / fiber.ts
Created August 6, 2019 08:30
Fiber (react hooks outside of react)
import { any } from "prop-types";
type Cell = { initialized: boolean; state: any; next: any };
export function Cell(): Cell {
return { initialized: false, state: null, next: null };
}
export type CellFunction = <State, Initial>(
callback: (state: State | Initial) => State,
@freddi301
freddi301 / statefulObservable.ts
Created June 24, 2019 13:57
Typescripty Stateful Observable
/*
let a = 1
let b = 2
const u = plus(a, mul(b,b))
const f = x => plus(a, x)
const k = f(b)
@freddi301
freddi301 / node.rs
Created June 21, 2019 15:52
Rust hash prefix tree
extern crate crypto;
use self::crypto::digest::Digest;
use self::crypto::sha2::Sha256;
use std::mem;
use std::rc::Rc;
fn main() {
let mut hasher = Sha256::new();
// write input message
@freddi301
freddi301 / ipfs-setup.sh
Last active June 18, 2019 15:16
Setup your IPFS node on digital ocean ubuntu
# You must run these command manually
mkdir ipfs
cd ipfs
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
source ~/.profile
nvm install stable
nvm use stable
npm install -g yarn
@freddi301
freddi301 / named-curry.ts
Created June 8, 2019 01:25
Typescript named curry functino
type Tequal<A, B> = A extends B ? (B extends A ? true : false) : false;
type Bind<P extends Record<string, any>, R> = Tequal<P, {}> extends false
? (<K extends keyof P>(
key: K,
value: P[K]
) => Bind<Pick<P, Exclude<keyof P, K>>, R>)
: () => R;
function curryNamed<Params extends Record<string, any>, R>(