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
export const equals = <A, K extends keyof A>(field: K, val: A[K]): Filter<A> => ({ | |
kind: "Equals", | |
field, | |
val | |
}); | |
export const greater = <A, K extends keyof A>(field: K, val: A[K]): Filter<A> => ({ | |
kind: "Greater", | |
field, | |
val | |
}); |
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
{ | |
kind: 'And', | |
a: { kind: 'Equals', field: 'manager', val: 'Bob Slydell' }, | |
b: { kind: 'Greater', field: 'salary', val: 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
type JobPosting { | |
manager: string; | |
salary: number; | |
} | |
const filter: Filter<JobPosting> = | |
and( | |
equals("manager", "Bob Slydell"), | |
greater("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
export type Filter<A> = | |
| { kind: "Equals"; field: keyof A; val: A[keyof A] } | |
| { kind: "Greater"; field: keyof A; val: A[keyof A] } | |
| { kind: "Less"; field: keyof A; val: A[keyof A] } | |
| { kind: "And"; a: Filter<A>; b: Filter<A> } | |
| { kind: "Or"; a: Filter<A>; b: 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
type Monoid<A> = { empty: A; append: (x: A, y: A) => A }; | |
const sum: Monoid<number> = { empty: 0, append: (x, y) => x + y }; | |
interface OrderType { | |
period: "ytd" | "mtd"; | |
orders: number; | |
} | |
const data: OrderType[] = [ | |
{ period: "ytd", orders: 3 }, |
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 { Filter } from "./filterDsl" | |
// a sample data model | |
interface JobPosting { | |
manager: string; | |
position: string; | |
} | |
//a filter of the data model | |
const query: Filter<JobPosting> = and( |
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
module Main where | |
import Prelude | |
import Effect (Effect) | |
import Effect.Console (log) | |
data Heading = North | South | East | West | |
data Position = Position | |
{ x ∷ Int |
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 data structure with our core commands and type | |
export type DslC = | |
| { kind: "position"; a: Position } | |
| { kind: "forward"; a: DslC } | |
| { kind: "right"; a: DslC }; | |
export const pos = (a: Position): DslC => ({ |