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 heading = | |
| North | |
| South | |
| East | |
| West; | |
type position = { | |
x: int, | |
y: int, | |
heading, |
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 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 | |
) |
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 ordering = | |
| Greater | |
| Less | |
| Equal; | |
// ORD typeclass / interface module | |
module type ORD = { | |
type t; | |
let compare: (t, t) => ordering; | |
}; |
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
// | |
// 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 |
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
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"); |
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
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]; |
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
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 } |
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
//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> => |
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 query: Filter<JobPosting> = and( | |
equals("manager", "Bob Slydell"), | |
greater("salary", 50000) | |
); | |
console.log( | |
interpretToSql(query) | |
) | |
// ([manager] = 'Bob Slydell' and [salary] > '50000') |
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 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)})`; |
NewerOlder