Skip to content

Instantly share code, notes, and snippets.

Reid Evans reidev275

Block or report user

Report or block reidev275

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View dsl.ts
export type Heading = "north" | "south" | "east" | "west";
export type Position = { heading: Heading; x: number; y: number };
export type DslC =
| { kind: "position"; a: Position }
| { kind: "forward"; a: DslC }
| { kind: "right"; a: DslC };
export const pos = (a: Position): DslC => ({
kind: "position",
View validateComplexModel.ts
import * as V from "../src/validation";
import * as PV from "../src/predValidation";
// a custom error type
interface ErrorType {
path: string;
error: string;
}
// some validations
View Validation.ts
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 => ({
@reidev275
reidev275 / Validation.ts
Last active Jul 24, 2019
Validation with Monoidal Contravariant Functors
View Validation.ts
// a type to contain information about validation results
export type Validation =
| { kind: "Success" }
| { kind: "Failure"; errors: string[] };
// helper functions to create Validation objects
const success: Validation = { kind: "Success" };
const failure = (errors: string[]): Validation => ({
kind: "Failure",
errors: errors
View reducerWithAsync.tsx
import React from "react";
const delay = () => new Promise((res, rej) => setTimeout(res, 2000));
type Model = { count: number };
type Message = "increment" | "decrement" | "sendDelay";
const Counter: React.FC<Model> = (initial: Model) => {
const [model, emit] = React.useReducer((m: Model, msg: Message): Model => {
switch (msg) {
View Counter.tsx
import * as React from "react";
import { Tea } from "./tea";
type Props = { start?: number };
type Message = "increment" | "decrement";
type Model = { count: number };
export class Counter extends Tea<Props, Message, Model> {
constructor(props: Props) {
super(props);
View App.tsx
import React, { Component } from "react";
import { FormModel, Form } from "./Form";
type Person = {
firstName: string;
lastName: string;
birthdate: Date | undefined;
gender: "Male" | "Female" | "Trans" | "Other" | "";
children: number;
};
@reidev275
reidev275 / api.ts
Last active Apr 17, 2019
Type safe api and client. By defining an interface, some mapped types, and some generic helpers we're able to interpret the interface into a type safe server and client library.
View api.ts
//code that exists in the server project
import { AsEndpoint, Handler, generateClient } from "./endpoint"
import * as express from "express";
//Our Api for communication between server and client
export interface Api {
getPokemonByName(name: string): Pokemon | undefined;
allPokemon(): Pokemon[];
}
View Form.ts
type Input<A> = { title: string; value: A };
type Form<A> = { [P in keyof A]: Input<A[P]> };
//An example model
type Person = {
firstName: string;
lastName: string;
children: number;
};
View interpretDslProgram.purs
program State Dsl State
program init =
forward init
>>= left
>>= forward
>>= right
>>= forward
>>= left
>>= forward
>>= backward
You can’t perform that action at this time.