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 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
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 => ({ |
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 * as V from "../src/validation"; | |
import * as PV from "../src/predValidation"; | |
// a custom error type | |
interface ErrorType { | |
path: string; | |
error: string; | |
} | |
// some validations |
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 { Monoid } from "./monoid"; | |
// a type to contain information about validation results | |
export type Validation = | |
| { kind: "Success" } | |
| { kind: "Failure"; errors: string[] }; | |
// helper functions to create Validation objects | |
export const success: Validation = { kind: "Success" }; | |
export const failure = (errors: string[]): Validation => ({ |