This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_]; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
NewerOlder