Skip to content

Instantly share code, notes, and snippets.

Avatar

Dennis Snell dmsnell

View GitHub Profile
@dmsnell
dmsnell / counter.mjs
Created Aug 29, 2020
50k `async` JS processes
View counter.mjs
export const counter = async ({send, receive}) => {
let count = 0;
while (1) {
const [action, args] = await receive();
switch (action) {
case 'inc':
count++;
break;
@dmsnell
dmsnell / demo.js
Last active Aug 27, 2020
Process Primitives
View demo.js
const { spawn, send } = require('./process.js')
const counter = spawn( async (receive) => {
let count = 0;
while (1) {
const message = await receive();
if (message === 'inc') {
console.log(++count);
@dmsnell
dmsnell / sequenced-async.ts
Created Aug 17, 2020
JS worker processing sync and async inbound messages sequentially, inspired by Erlang gen_server
View sequenced-async.ts
import { MessageChannel, MessagePort } from 'worker_threads';
type ServerDescription<K extends string> = {
[type in K]: (...args: unknown[]) => unknown;
}
type ServerHandler<M extends ServerDescription<keyof M>, S> = <T extends keyof M>(msg: T, state: S) => [ReturnType<M[T]>, S] | Promise<[ReturnType<M[T]>, S]>;
class GenericServer<M extends ServerDescription<keyof M>, S = undefined> {
count = 0;
@dmsnell
dmsnell / tag-hasher.java
Last active Jul 5, 2020
Simplenote Tag Hashing Functions - generate tag entity id from tag name
View tag-hasher.java
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.Normalizer;
import java.util.Locale;
class TagHasher {
public static String tagHash(String tagName) {
try {
String normalized = Normalizer.normalize(tagName, Normalizer.Form.NFC);
String lowercased = normalized.toLowerCase(Locale.ENGLISH);
@dmsnell
dmsnell / BlockParser.java
Last active Apr 12, 2020
Native Java BlockParser for Gutenberg Posts
View BlockParser.java
import java.util.ArrayList;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
class BlockParser {
@dmsnell
dmsnell / formula-block-editor.js
Last active Sep 9, 2019
Formula-entry and mathematical typesetting block
View formula-block-editor.js
const { registerBlockType } = wp.blocks;
const { TextareaControl } = wp.components;
const { createElement: el } = wp.element;
const attributes = {
formula: { type: 'string' },
rendered: {
type: 'string',
source: 'attribute',
selector: 'img',
@dmsnell
dmsnell / Main.elm
Created Apr 4, 2019
Using the URL fragment/query args to determine which "sub app" to render, using initial URL to inform state
View Main.elm
module Main exposing (main)
import Browser as B
import Browser.Navigation as BN
import Html as H
import Html.Attributes as HA
import Html.Events as HE
import Url as U
@dmsnell
dmsnell / Dockerfile
Last active Mar 29, 2019
WordPress Core Unit Tests
View Dockerfile
FROM ubuntu
WORKDIR /tmp
ENV PACKAGES="\
composer \
curl \
git \
imagemagick \
locales-all \
@dmsnell
dmsnell / Block.elm
Created Nov 21, 2018
Gutenberg blocks written in Elm
View Block.elm
port module Main exposing (Flags, Model, Msg(..), init, main, subscriptions, update, view)
import Browser
import Html exposing (Html, button, div, input, text)
import Html.Attributes exposing (type_)
import Html.Events exposing (onClick, onInput)
port setAttributes : Flags -> Cmd msg
port getAttributes : (Flags -> msg) -> Sub msg
@dmsnell
dmsnell / regexpCollapser.js
Created Jun 30, 2018
collapses alternation groups in a regex
View regexpCollapser.js
const trie = require('trie-prefix-tree');
const tldList = require('tlds');
//const corpus = tldList;
const corpus = [ 'ca', 'cat', 'cot', 'car', 'catastrophe', 'catastrophic', 'dog' ];
//const corpus = [ 'cat' ];
const tlds = trie( corpus ).tree();
//