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 / merge_sorted.ts
Created August 29, 2018 20:05
merge two sorted arrays O(n)
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]
@venil7
venil7 / pairs.js
Created August 29, 2018 19:01
pairs of array that sum to X, O(n)
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]);
@venil7
venil7 / ReducerComponent.re
Created August 26, 2018 13:24
ReasonReact reducer component boilerplate
type state = {a: int};
type action =
| Remove
| Add
| Update;
let component = ReasonReact.reducerComponent("ReducerComponent");
let make = _children => {
...component,
initialState: () => 1,
@venil7
venil7 / maybe.ts
Created May 11, 2018 11:35
Maybe monad in TypeScript
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;
@venil7
venil7 / unlim-arity.ts
Last active May 4, 2018 19:50
Unlimited arity curried computation
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
@venil7
venil7 / reader-monad.ts
Last active August 29, 2022 03:19
Reader Monad in TypeScript
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);
});
}
@venil7
venil7 / state-monad.ts
Last active January 31, 2020 13:06
State Monad in TypeScript
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) => {
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) {
@venil7
venil7 / hanoi-tower.re
Created March 10, 2018 17:45
quick implementation of the Tower Of Hanoi problem
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])
@venil7
venil7 / list.ts
Last active September 19, 2019 18:07
TypeScript Linked List (Haskell/OCaml)
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: {