Skip to content

Instantly share code, notes, and snippets.

type heading =
| North
| South
| East
| West;
type position = {
x: int,
y: int,
heading,
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
)
type ordering =
| Greater
| Less
| Equal;
// ORD typeclass / interface module
module type ORD = {
type t;
let compare: (t, t) => ordering;
};
//
// 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
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 }
//greater than or equal
export const gte = <A, K extends keyof A>(field: K, val: A[K]): Filter<A> =>
or(greater(field, val), equals(field, val));
//less than or equal
export const lte = <A, K extends keyof A>(field: K, val: A[K]): Filter<A> =>
or(less(field, val), equals(field, val));
//combine 1 to many filters returning true if all are true (and)
export const all = <A>(...dsl: Filter<A>[]): Filter<A> =>
const query: Filter<JobPosting> = and(
equals("manager", "Bob Slydell"),
greater("salary", 50000)
);
console.log(
interpretToSql(query)
)
// ([manager] = 'Bob Slydell' and [salary] > '50000')
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)})`;