Skip to content

Instantly share code, notes, and snippets.

View h2.js
let c = Symbol();
h = (tag, attrs = {}, children = [], node = document.createElement(tag)) => {
node[c] = children;
Object.entries(attrs).map(([k, v]) =>
/^on/.test(k)
? node.addEventListener(k.slice(2), v)
: node.setAttribute(k, v),
);
return node;
};
View sexpr.js
const whitespaceSymbol = Symbol('whitespace');
const whitespace = rawWhitespace.map(() => whitespaceSymbol);
const param = (param) => composeParsers([param, whitespace]);
// const
const sexpr = (fn) => {
const [head, ...tail] = fn();
const params = sequenceOf([head, ...tail.map(param)]);
View index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Static Template</title>
</head>
<body>
<script>
View bare-words.js
r=(fn) => {
try {
return fn();
} catch (e) {
// if (e instanceof ReferenceError) {
if (/^Re/.test(e+[])) {
globalThis[q=e.message.split` `[0]]=q;
return r(fn)
} else {
throw e;
View h.js
function h(tag, attrs = {}, children = []) {
const node = document.createElement(tag);
node.__children = children;
Object.entries(attrs).forEach(([k, v]) => {
/^on/.test(k)
? node.addEventListener(k.slice(2), v)
: node.setAttribute(k, v);
});
return node;
}
View conditional-display.js
const {
str,
char,
digits,
letters,
sequenceOf,
anythingExcept,
anyOfString,
many,
choice,
View confetti.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>Fetti</title>
<style>
div {
left: 50%;
right: 50%;
View combineProviders.js
function combineProviders(providers) {
return class Combined extends React.Component {
render() {
return (
providers.reverse().reduce((acc, Provider) => (
<Provider>{acc}</Provider>
), this.props.children)
);
}
}
View contexthook.js
// custom hook: useStore
const ctx = createContext();
export const Store = ({ children }) => {
const [state, setState] = useState(initialState);
const mergeState = useCallback((obj) => {
setState(state => ({ ...state, ...obj }));
}, []);
View calc.rs
#[derive(Debug, Clone, PartialEq)]
enum Token {
Op(char),
Num(f64),
}
fn prec(op: char) -> usize {
match op {
'*' | '/' => 3,
'+' | '-' => 2,