Skip to content

Instantly share code, notes, and snippets.

View venil7's full-sized avatar
turning coffee into code

Art Deineka venil7

turning coffee into code
View GitHub Profile
@venil7
venil7 / spiral.ts
Created January 20, 2021 20:34
traverse matrix in spiral
const a = [
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
];
enum Dir {
east, south, west, north
@venil7
venil7 / mergesort.ts
Created June 2, 2020 19:42
Merge Sort (TypeScript)
const sort = (a: number[]): number[] => {
const split = (a: number[]): [number[], number[]] => {
const take_left = Math.ceil(a.length / 2);
const take_right = Math.ceil(a.length / 2);
const left = a.slice(0, take_left);
const right = a.slice(take_right);
return [left, right];
};
const merge = (a: number[], b: number[]): number[] => {
@venil7
venil7 / state_monad.ts
Last active May 24, 2020 17:37
TypeScript State Monad
type StateM<S, T> = {
runState: (state: S) => T,
then: <U>(func: (t: T, pure: <U>(u: U) => StateM<S, U>) => StateM<S, U>) => StateM<S, U>,
};
const stateM = function <S, T>(runState: (state: S) => [S, T]): StateM<S, T> {
return {
runState: function (s: S): T {
const [, t] = runState(s);
return t;
@venil7
venil7 / maybe_monad.ts
Last active May 24, 2020 10:22
TypeScript Maybe<T> monad
type Maybe<T> = {
map: <T2>(func: (t: T) => T2) => Maybe<T2>,
then: <T2>(func: (t: T) => Maybe<T2>) => Maybe<T2>,
value: (fallback: T) => T
};
const maybe = function <T>(a: T | undefined | null): Maybe<T> {
const value: [T] | [] = (a !== null && a !== void 0) ? [a] : [];
return {
map: function <T2>(func: (t: T) => T2): Maybe<T2> {
return value.length
@venil7
venil7 / last_elem_type.ts
Created June 26, 2019 20:18
infer type of last element in array
type LastElem<T extends number> = [
-1,0,1,2,3,4,5
][T];
type A = [number,string, boolean];
type Z = A[LastElem<A['length']>] //boolean
@venil7
venil7 / maybe_functor.ts
Last active May 7, 2019 11:17
simple maybe functor
class Maybe<T> {
constructor(private val: T | null|undefined) { }
public get hasVal():boolean {
return (this.val !== null || this.val !== undefined);
}
public value(fallback: T):T {
return this.hasVal ? this.val : fallback;
}
public map<U>(func:(t: T) => U): Maybe<U> {
return this.hasVal
@venil7
venil7 / redux-observable.js
Created March 14, 2019 21:27
a quick implementation of redux-obesrvable middleware
import { Subject } from "rxjs";
export const createMiddleware = () => {
const [action$, state$] = [new Subject(), new Subject()];
const middleware = store => {
return dispatch => {
middleware.run = epic => epic(action$, state$).subscribe(store.dispatch);
return action => {
const result = dispatch(action);
action$.next(action);
@venil7
venil7 / lense.ts
Last active May 24, 2020 11:08
Functional Lense implementation in TypeScript
type LGetter<LFrom, LTo> = (f: LFrom) => LTo;
type LSetter<LFrom, LTo> = (t: LTo, f: LFrom) => LFrom;
type LUpdater<T> = ((t: T) => T);
type LModifier<LFrom, LTo> = (u: LUpdater<LTo>) => (f: LFrom) => LFrom;
class Lense<LFrom, LTo> {
constructor(private get: LGetter<LFrom, LTo>, private set: LSetter<LFrom, LTo>) { }
public compose<LTo2>(inner: Lense<LTo, LTo2>): Lense<LFrom, LTo2> {
const _get: LGetter<LFrom, LTo2> = (f: LFrom) => inner.get(this.get(f));
const _set: LSetter<LFrom, LTo2> = (t2: LTo2, f: LFrom) => this.set(inner.set(t2, this.get(f)), f);
@venil7
venil7 / useMiddleware.js
Created November 30, 2018 13:24
Use redux middle with React hooks
const useMiddleware = (middlewares, reducer, initState) => {
const [state, dispatch] = useReducer(reducer, initState);
const getStore = () => ({ dispatch, getState: () => state });
const store = applyMiddleware(...middlewares)(getStore)();
const { dispatch: dispatch_, getState } = store;
return [getState(), dispatch_];
};
@venil7
venil7 / useThunk.js
Created November 30, 2018 11:23
Using thunk with React hooks
const useThunk = (reducer, initState) => {
const [state, dispatch] = useReducer(reducer, initState);
const thunkDispatch = action => {
if (typeof action === "function") {
action(dispatch, () => state);
} else {
dispatch(action);
}
};
return [state, thunkDispatch];