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
data Vec = Vec [Int] | |
deriving (Show, Eq) | |
instance Monoid Vec where | |
mempty = (Vec mempty) | |
mappend = vecAdd | |
vecAdd (Vec a) (Vec b) = Vec $ zipWith (+) a b | |
vecSubtract (Vec a) (Vec b) = Vec $ zipWith (-) a b |
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
//BatchedQueue implementation from Chirs Okasaki's "Purely Functional Data Structures" | |
import { fromNullable } from "fp-ts/lib/Either"; | |
import { Foldable1 } from "fp-ts/lib/Foldable"; | |
import { Monad1 } from "fp-ts/lib/Monad"; | |
import { getArraySetoid, Setoid } from "fp-ts/lib/Setoid"; | |
const URI = "Queue"; | |
type URI = typeof URI; |
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
//BatchedQueue implementation from Chirs Okasaki's "Purely Functional Data Structures" | |
class Queue<A> { | |
constructor(public readonly f: A[], public readonly t: A[]) {} | |
} | |
export const empty = new Queue([], []); | |
export const isEmpty = <A>(q: Queue<A>) => q.f.length === 0; | |
/** |
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
export const findFirst = <A>(predicate: (a: A) => boolean) => (t: Tree<A>): Option<A> => { | |
const ff = findFirst(predicate); | |
return t.fold( | |
() => none, | |
(l, v, r) => | |
ff(l) | |
.orElse(() => ff(r)) | |
.orElse(() => (predicate(v) ? some(v) : none)) | |
); | |
}; |
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 { Alternative1 } from "fp-ts/lib/Alternative"; | |
import { Monad1 } from "fp-ts/lib/Monad"; | |
import { Monoid } from "fp-ts/lib/Monoid"; | |
import * as most from "most"; | |
declare module "most" { | |
interface Stream<A> { | |
_URI: URI; | |
_A: 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
import { Apply1 } from "fp-ts/lib/Apply"; | |
import { Type, URIS } from "fp-ts/lib/HKT"; | |
import { Task, task } from "fp-ts/lib/Task"; | |
import { Stream, zip } from "most"; | |
import { append } from "ramda"; | |
import { Applicative1 } from "fp-ts/lib/Applicative"; | |
export const sequenceTaskArray = <T>(ts: Task<T>[]): Task<T[]> => | |
ts.reduce((acc, t) => { | |
const y = t.map(x => (ts: T[]) => append(x, ts)); |
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 liftA2 = <T, R>(f: (x: T) => (y: T) => R) => (x: Maybe<T>) => (y: Maybe<T>): Maybe<R> => y.ap(x.map(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 { Maybe } from "monet"; | |
/** | |
* Turns Maybe<T>[] into Maybe<T[]> | |
* This is basically Ramda's sequence; http://ramdajs.com/docs/#sequence | |
* But their types don't let us use it *angryface* | |
* @param xs List of maybes | |
*/ | |
export function sequence<T>(xs: Maybe<T>[]): Maybe<T[]> { | |
return xs.reduce<Maybe<T[]>>((acc, x) => { |
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 Primitive = string | number | boolean | Symbol | null | undefined; | |
const isObject = (x: any): x is Object => R.is(Object, x); | |
const isPrimitive = (x: any): x is Primitive => !isObject(x); | |
const deepMergeWith = <T>(fn: <U extends Primitive>(a: U, b: U) => U) => (a: T, b: T): T => { | |
//base case | |
if (isPrimitive(a) && isPrimitive(b)) { | |
return fn(a, b); | |
} | |
//inductive step |
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 { Stream } from "most"; | |
import * as R from "ramda"; | |
declare module "most" { | |
interface Stream<A> { | |
distinctUntilChange<T>(seed: T): Stream<T>; | |
} | |
} | |
const pairs = <P, C>(prev: P, current: C) => ({ seed: current, value: [prev, current] }); |