Skip to content

Instantly share code, notes, and snippets.

View Lucifier129's full-sized avatar
🎯
Focusing

工业聚 Lucifier129

🎯
Focusing
View GitHub Profile
@Lucifier129
Lucifier129 / procedural-abstraction.ts
Created August 16, 2020 03:59
procedural abstraction
// data version
type DraftPost = {
kind: 'DraftPost';
content: string;
};
type ReviewingPost = {
kind: 'ReviewingPost';
content: string;
export const fetch = (url, options = {}) => {
let { fetch = globalThis.fetch, ...restOptions } = options;
if (typeof fetch !== 'function') {
throw new Error(`Expected fetch to be a function, but received ${fetch}`);
}
let finalUrl = url;
let finalOptions = {
@Lucifier129
Lucifier129 / action-pattern-matching.ts
Created May 19, 2020 01:28
match action of reducer via codata & visitor-pattern
type State = {
count: number;
};
type ActionVisitor = {
incre: (step: number) => State;
decre: (step: number) => State;
};
type Action = {
@Lucifier129
Lucifier129 / foop.ts
Last active May 4, 2020 14:15
functional oop
// option type
interface OptionVisitor<A, B> {
None: () => B;
Some: (a: A) => B;
}
type OptionData<A> =
| {
kind: 'None';
}
let identity = x => x
let createBF = (getChildren) => {
let handler = (stack = [], results = [], f) => {
if (stack.length === 0) return results;
let [head, ...tail] = stack;
let nextStack = tail.concat(getChildren(head));
return handler(nextStack, [...results, f(head)], f);
};
return (tree, f = identity) => handler([tree], [], f);
};
const createTreeModule = (operations) => {
const append = (tree, node) => {
if (operations.matchChild(tree, node)) {
return operations.appendChild(tree, node);
}
return operations.map(tree, (child) => {
return append(child, node);
});
};
@Lucifier129
Lucifier129 / algebraic-effects.js
Created April 23, 2020 02:30
poor man's algebraic-effects via throw + hand-written continuations
let run = (f, handlers) => {
try {
return f();
} catch (effect) {
if (effect.type in handlers) {
handlers[effect.type](effect.payload, effect.continuation);
} else {
throw effect
}
}
const createOperation = name => (...args) => {
let operation = interpreter =>
interpreter[name](
...args.map(f => (f && f.operation ? f(interpreter) : f))
);
operation.operation = true;
return operation;
};
const createOperations = (...names) =>
@Lucifier129
Lucifier129 / json-parser.purs
Last active October 31, 2019 07:23
naive parser combinator written by purescript
module Parser where
import Prelude (($), (<>), (==), (>=), (<=), (&&), (/=), (||), (>>>), negate)
import Data.Show (class Show, show)
import Data.Tuple (Tuple(..))
import Data.Either (Either(..))
import Data.Array ((:), intercalate)
import Data.Foldable (foldl, foldr)
import Data.Maybe (Maybe(..))
import Data.String (codePointAt)
type result('a, 'b) =
| Ok('a)
| Error('b)
let inject = value => input => (Ok(value), input)
let error = e => input => (Error(e), input)
let item = input => {
let (index, source) = input