This file contains hidden or 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 ReaderMonad<A, B> { | |
| constructor(public runReader: (a: A) => B) { | |
| } | |
| bind<C>(func: (b: B) => ReaderMonad<A, C>): ReaderMonad<A, C> { | |
| return new ReaderMonad<A, C>((a: A) => { | |
| const b = this.runReader(a); | |
| return func(b).runReader(a); | |
| }); | |
| } |
This file contains hidden or 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 aux = (f: (a,b) => number, acc: number) => | |
| (n?: number) => (n !== undefined) ? aux(f, f(acc, n)) : acc; | |
| const add = (a, b) => a + b; | |
| const mul = (a, b) => a * b; | |
| const sum = aux(add, 0); | |
| const product = aux(mul, 1); | |
| const sum1 = sum(2)(3)(5)(); //10 |
This file contains hidden or 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) { } | |
| with<Z>(f: (z: T) => Z): Maybe<Z> { | |
| return new Maybe(this.return(f)); | |
| } | |
| return<Z>(f: (z: T) => Z): Z { | |
| try { | |
| return (f(this.val)); | |
| } catch (err) { | |
| return undefined; |
This file contains hidden or 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 state = {a: int}; | |
| type action = | |
| | Remove | |
| | Add | |
| | Update; | |
| let component = ReasonReact.reducerComponent("ReducerComponent"); | |
| let make = _children => { | |
| ...component, | |
| initialState: () => 1, |
This file contains hidden or 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
| let pairs = (arr: number[], s: number): number[][] => { | |
| let cache = {}; | |
| let res = []; | |
| for (let i of arr) { | |
| if (!cache[i]) { | |
| cache[i] = true; | |
| } | |
| let snd = s - i; | |
| if (cache[snd]) { | |
| res.push([i, snd]); |
This file contains hidden or 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 merge = (a: number[], b: number[]): number[] => { | |
| let length = a.length + b.length; | |
| let result = Array(length); | |
| let i = 0, j = 0, k = 0; | |
| while (i < a.length && j < b.length) { | |
| if (a[i] < b[j]) { | |
| result[k] = a[i]; | |
| i += 1; | |
| } else if (a[i] > b[j]) { | |
| result[k] = b[j] |
This file contains hidden or 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]; |
This file contains hidden or 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 hidden or 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 hidden or 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); |