Skip to content

Instantly share code, notes, and snippets.

//
// Prelude
// A zero dependency, one file drop in for faster Typescript development with fewer bugs
// through type safe, functional programming. Comments are inline with links to blog posts motiviating the use.
// alias for a function with airity 1
export type Fn<A, B> = (a: A) => B;
// alias for a function with airity 2
const interpretToSql = <A>(dsl: Filter<A>): string => {
switch (dsl.kind) {
case "Equals":
return `[${dsl.field}] = '${dsl.val}'`;
case "Greater":
return `[${dsl.field}] > '${dsl.val}'`;
case "Less":
return `[${dsl.field}] < '${dsl.val}'`;
case "And":
return `(${interpretToSql(dsl.a)} and ${interpretToSql(dsl.b)})`;
export const contramap = <A, B>(f: (b: B) => A, O: Ord<A>): Ord<B> => ({
compare: (x: B, y: B) => O.compare(f(x), f(y))
});
const stringOrd: Ord<string> = ({
compare: (x: string, y: string) => x < y ? Ordering.LT : x > y ? Ordering.GT : Ordering.EQ
})
//extending stringOrd to work on Person objects
const firstName: Ord<Person> = contramap(x => x.first, stringOrd);
type heading =
| North
| South
| East
| West;
type position = {
x: int,
y: int,
heading,
type ordering =
| Greater
| Less
| Equal;
// ORD typeclass / interface module
module type ORD = {
type t;
let compare: (t, t) => ordering;
};
let pure a = Some a |> Js.Promise.resolve
let fail _ = None |> Js.Promise.resolve
let (>=>) x y a =
x a
|> Js.Promise.then_ (
function
| None -> None |> Js.Promise.resolve
| Some s -> y s
)
@reidev275
reidev275 / Monoid.cs
Last active July 15, 2020 14:52
Monoid and Foldable for C#
public interface Monoid<A>
{
A Empty { get; }
A Append(A x, A y);
}
public static class Foldable
{
public static A Fold<A>(this IEnumerable<A> list, Monoid<A> M)
{
import { retryWithDelay } from "solemnlySwear"
// a promise returning function that succeeds apprx 1 in 10 times
const fakePromise = () => {
const randInt = (min: number, max: number): number =>
Math.floor(Math.random() * (max - min + 1)) + min;
const x = randInt(1, 10);
return new Promise((res, rej) => {
if (x === 1) {
res("Success");
import { Parser } from "json2csv";
const csv = require("csvtojson");
const fs = require("fs").promises;
import { Config as MssqlConfig, execute } from "lesstedious";
export type Dsl<A> = () => Promise<Array<A>>;
export const union = <A>(x: Dsl<A>, y: Dsl<A>): Dsl<A> => async () => {
const [x2, y2] = await Promise.all([x(), y()]);
return [...x2, ...y2];
export type Heading = "north" | "south" | "east" | "west";
export type Position = {
heading: Heading;
x: number;
y: number;
};
//recursive, sum type data structure with our core commands and type
export type DslC =
| { kind: "position"; a: Position }