Skip to content

Instantly share code, notes, and snippets.

View Lucifier129's full-sized avatar
🎯
Focusing

工业聚 Lucifier129

🎯
Focusing
View GitHub Profile
type Result<T> = { ok: true; value: T } | { ok: false; error: string };
const Ok = <T>(value: T): Result<T> => ({ ok: true, value });
const Fail = (error: string): Result<any> => ({ ok: false, error });
class Accessor<S, T> {
constructor(
public readonly get: (s: S) => Result<T>,
public readonly set: (t: T, s: S) => Result<S>
) { }
type Result<T> = { ok: true; value: T } | { ok: false; error: string };
const Ok = <T>(value: T): Result<T> => ({ ok: true, value });
const Fail = (error: string): Result<any> => ({ ok: false, error });
class Accessor<S, T> {
  constructor(
    public readonly get: (s: S) => Result<T>,
    public readonly set: (t: T, s: S) => Result<S>
  ) { }
@Lucifier129
Lucifier129 / codata-lens-state-machine.ts
Created July 13, 2022 08:12
Modular state-management via codata & lens & state-machine
type PrimitiveData = string | number | boolean | null
type ListData = LensData[]
type ObjectData = {
[key: string]: LensData
}
type LensData = PrimitiveData | ListData | ObjectData
@Lucifier129
Lucifier129 / push-stream.ts
Created July 8, 2022 09:53
push-stream via codata
function pipe<A, B>(a: A, f: (a: A) => B): B;
function pipe<A, B, C>(a: A, f: (a: A) => B, g: (b: B) => C): C;
function pipe<A, B, C, D>(
a: A,
f: (a: A) => B,
g: (b: B) => C,
h: (c: C) => D
): D;
function pipe<A, B, C, D, E>(
a: A,
@Lucifier129
Lucifier129 / pull-stream.ts
Last active July 7, 2022 09:40
pull-stream via codata
function pipe<A, B>(a: A, f: (a: A) => B): B;
function pipe<A, B, C>(a: A, f: (a: A) => B, g: (b: B) => C): C;
function pipe<A, B, C, D>(
a: A,
f: (a: A) => B,
g: (b: B) => C,
h: (c: C) => D
): D;
function pipe<A, B, C, D, E>(
a: A,
use std::collections::HashMap;
extern crate peg;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Variable(String);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Value {
Integer(i32),
Variable(Variable),
// library
type Handler<T, R> = [new (...args: any) => T, (input: T) => R];
type HandlerPattern<T extends Handler<any, any>> = T extends Handler<
infer Pattern,
any
>
? Pattern
: unknown;
type Prettier<T> = {
[key in keyof T]: T[key];
}
type Tagged<Tag extends string> = {
tag: Tag;
};
type Field<Key extends string, Value> = {
[key in Key]: Value;
@Lucifier129
Lucifier129 / todo-app.remesh
Created November 16, 2021 07:38
A showcase of Remesh DSL
domain TodoInputDomain {
state TodoInput = ''
command updateTodoInput(newTodoInput: string) {
return TodoInput.new(newTodoInput)
}
}
type Todo = {
@Lucifier129
Lucifier129 / merge-type.ts
Created September 5, 2021 11:56
conditional union/intersection types
// type utils
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (
x: infer R
) => any
? R
: never;
type UnionOf<T extends object> = T[keyof T];
type IntersectionOf<T extends object> = UnionToIntersection<UnionOf<T>>;