Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

工业聚 Lucifier129

🎯
Focusing
View GitHub Profile
View project-level-fetch-api.js
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
match action of reducer via codata & visitor-pattern
View action-pattern-matching.ts
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
functional oop
View foop.ts
// option type
interface OptionVisitor<A, B> {
None: () => B;
Some: (a: A) => B;
}
type OptionData<A> =
| {
kind: 'None';
}
View Breadth-First.js
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);
};
View tree-module.js
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 Apr 23, 2020
poor man's algebraic-effects via throw + hand-written continuations
View algebraic-effects.js
let run = (f, handlers) => {
try {
return f();
} catch (effect) {
if (effect.type in handlers) {
handlers[effect.type](effect.payload, effect.continuation);
} else {
throw effect
}
}
View untyped-tagless-final-interpreters.js
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 Oct 31, 2019
naive parser combinator written by purescript
View json-parser.purs
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)
View json-parser.re
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
@Lucifier129
Lucifier129 / codata.ts
Last active Jul 24, 2020
some codata examples in javascript/typescript
View codata.ts
interface BtreeVisitor<T, TT> {
leaf: (value: T) => TT;
branch: (left: TT, right: TT) => TT;
}
interface Btree<T> {
<TT>(visitor: BtreeVisitor<T, TT>): TT;
}
const leaf = <T>(value: T): Btree<T> => {
You can’t perform that action at this time.