Skip to content

Instantly share code, notes, and snippets.

View eczn's full-sized avatar
Star

eczn* eczn

Star
View GitHub Profile
@eczn
eczn / go-image-interface.go
Last active January 1, 2020 13:52
Go 语言 Image interface 实例
package main
import (
"golang.org/x/tour/pic"
"image"
"image/color"
)
type MyImage struct{
size int
@eczn
eczn / ts-copy-files.sh
Created January 2, 2020 12:52
typescript copy static files
copyfiles -u 1 src/**/*.js lib
@eczn
eczn / higher-order-lifter.ts
Created April 13, 2020 11:15
how to compose the existed Action / Reducer to make create multi instances on global redux store (combineReducer) in TypeScript
// how to compose the existed Action / Reducer to make create multi instances on global redux store (combineReducer)
// inspired from https://github.com/reduxjs/redux/issues/897
/** liftAction lifting your existed Action */
export function liftAction<LType extends string, Arg, A>(
/** lift key */
liftedType: LType,
/** original action generator */
actionGenerator: (...args: Arg[]) => A
/* considering react-thunk so i use `HIGHT_ORDER_ACTION` refers to `LType` */
@eczn
eczn / feature-support.tsx
Last active September 22, 2022 03:44
使用 React Hooks 完美解决滚动穿透问题 (兼容 Passive Event)
/** 特性支持表 */
export const FEATURES = new class {
/** Is Window Supports Passive Event */
public PASSIVE_EVENT = false;
public constructor() {
this.init();
}
public init() {
@eczn
eczn / react-flat.ts
Created August 25, 2020 13:11
2020 ts array flatten function implementation
export type Maybe<T> = null | T | Maybe<T>[] | { props?: { children?: Maybe<T> } };
export type ID<T> = (a: any) => a is T;
export const isNumber = ((x: any) => typeof x === 'number') as ID<number>; // 看来又是一个 ts shit
export function flat<T>(arr: Maybe<T>[], isT: ID<T>): T[] {
if (arr.length === 0) return [];
const [x, ...xs] = arr;
@eczn
eczn / use-time-machine.tsx
Last active September 6, 2020 12:42
An Implementation Of Time Machine For State By React Hooks
import React from 'react';
/** useTimeMachine option define */
export interface TimeMachineOption<T> {
/** outter changing timer */
machineTime: number,
/** time map to state */
getStateByTime: (time: number) => Promise<T>,
/** judge should update state by stateTime & machineTime */
shouldUpdateState: (stateTime: number, machineTime: number) => boolean;
@eczn
eczn / type-ioc.tsx
Last active December 15, 2022 12:47
TypeScript IoC(DI) 设计模式实践, 具体实现见 ioc.tsx, 用法见 usage.tsx
import 'reflect-metadata';
/** 此类型可描述任意 class */
type AnyClass<Args extends any[] = any, Instance = any> = new (...args: Args) => Instance;
/**
* ## IOC 控制器
* 支持做 IOC 设计模式
* - IOC.Register 注册类到 IOC 控制器
* - IOC.RegisterOne 注册类到 IOC 控制器 (单例模式)
@eczn
eczn / promise-machine.ts
Last active August 6, 2023 09:36
use promise as a immutable state machine (一种将 Promise 当成状态机使用的实践,爷非常喜欢,只有用过或者遇到过类似逻辑的人才懂)
/** a way to use promise as an immutable state machine */
export class PromiseMachine<T> {
/** this promise instance */
private $promise: Promise<T>;
/** make this resolved */
private $resolve!: (val: T) => void;
/** make this rejected */
private $reject!: (err: any) => void;
/** 全部が終わった */
@eczn
eczn / sum.rs
Last active October 21, 2020 08:51
rust: sum of integer in two computing style
// recursive style
fn sum(n: i32) -> i32 {
if n == 0 { 0 } else {
n + sum(n - 1)
}
}
// iterative style
fn sum2(n: i32) -> i32 {
fn iter(i: i32, accumulate: i32) -> i32 {
@eczn
eczn / rust_read_toml.rs
Last active February 1, 2023 01:28
read toml file in rust lang
use std::fs;
use std::io::Error;
#[derive(serde_derive::Deserialize)]
#[derive(Debug)]
struct MyConfDefine {
eczn_info: EcznInfo
}
#[derive(serde_derive::Deserialize)]