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 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 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 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 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 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 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 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 StateMonad<S, A> { | |
constructor(public runState: (s: S) => ({ s: S, a: A })) { | |
} | |
static return_<S, A>(a: A): StateMonad<S, A> { | |
return new StateMonad(s => ({ s, a })); | |
} | |
bind<B>(func: (a: A) => StateMonad<S, B>): StateMonad<S, B> { | |
return new StateMonad<S, B>((s: S) => { |
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
let next = (seq: list(int)) : int => | |
switch (seq) { | |
| [] => 0 | |
| [0] => 1 | |
| [x, y, ..._] => x + y | |
}; | |
let fib = (n: int) : list(int) => { | |
let rec fib' = (m: int, seq: list(int)) : list(int) => | |
switch (m) { |
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 rod = list(int); | |
exception InvalidMove(int, int); | |
exception OtherMove(rod, rod); | |
let moveRods = (a: rod, b: rod) : (rod, rod) => | |
switch (a, b) { | |
| ([x, ...xs], []) => (xs, [x]) | |
| ([x, ...xs], [y, ...ys]) when x < y => (xs, [x, y, ...ys]) |
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 Item<T> = { val: T, rest: List<T> } | null; | |
type List<T> = Item<T>; | |
const cons = <T>(val: T, rest: List<T>) => ({ val, rest }); | |
const empty = () => null; | |
const fold_left = <T, A>(func: (acc: A, val: T) => A, list: List<T>, acc: A): A => { | |
//poor man's pattern matching | |
switch (list) { | |
case empty(): return acc; | |
default: { |