Skip to content

Instantly share code, notes, and snippets.

View base32-encode.js
const crypto = require('crypto');
const charTbl = '0123456789abcdefghjkmnpqrstvwxyz';
const encodeChunk = (n) => charTbl[n];
const nthChunk = (input, idx) => {
const startBit = 5*idx;
const offset = startBit % 8;
const startByte = (startBit - offset)/8;
@kendru
kendru / queue.rs
Created Jul 10, 2020
Simple thread-safe bounded queue
View queue.rs
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::sync::mpsc;
use std::thread;
struct QueueInner<T: Send> {
items: VecDeque<T>,
is_done: bool,
parked_producers: VecDeque<mpsc::Sender<()>>,
}
@kendru
kendru / actors.cljs
Created Jun 14, 2020
Implementation of a very basic actor system
View actors.cljs
(ns notifications.actor
(:require [reagent.core :as r]
[cljs.core.async :refer [go-loop pub sub chan <! >! put! timeout]]))
(defn actor-system []
(atom {}))
(defn mailbox [address]
[address (chan)])
@kendru
kendru / recursive-callback-to-async.js
Created Jun 3, 2020
Example of converting a recursive callback to an asynchronous iterator
View recursive-callback-to-async.js
const promise_ = Symbol('promise');
class Deferred {
constructor() {
this[promise_] = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
get promise() {
@kendru
kendru / take-all.clj
Last active Mar 15, 2020
Clojure macro for taking values from a number of channels, short-circuiting on an error value
View take-all.clj
(defn is-error-clause? [expr]
(and (sequential? expr)
(= 'on-error (first expr))))
(defn parse-clauses [exprs]
(let [[body error-clauses] (partition-by is-error-clause? exprs)
[_ error-name & error-body] (first error-clauses)]
{:body body
:error-name error-name
:error-body error-body}))
@kendru
kendru / highlightText.js
Created Feb 21, 2020
Add case-insensitive non-overlapping highlights to text
View highlightText.js
function highlightText(text, highlights) {
const lcText = text.toLowerCase();
highlights = highlights.map(h => h.toLowerCase());
let addedChars = 0;
for (let i = 0; i < text.length; i++) {
for (const highlight of highlights) {
if (
lcText[i] === highlight[0] &&
lcText.substring(i, i + highlight.length) === highlight
@kendru
kendru / postgres-sandbox.sh
Created Jan 18, 2020
Set up a throw-away postgresql server in docker and a client to connect to it
View postgres-sandbox.sh
#!/bin/bash
docker pull postgres:11-alpine
docker network inspect postgres_sandbox > /dev/null 2>&1
if [[ "$?" == "0" ]]; then
docker network rm postgres_sandbox
fi
docker network create --driver bridge postgres_sandbox
@kendru
kendru / objects.clj
Created Nov 18, 2019
Emulating Objects in Clojure(Script)
View objects.clj
(defn make-mailbox
([] (make-mailbox {:messages []
:next-id 1}))
([state]
{:deliver!
(fn [msg]
(make-mailbox
(-> state
(update :messages conj
(assoc msg :read? false
@kendru
kendru / f97d9211-26b2-495a-a872-bc91a4873f18
Created Oct 24, 2019
f97d9211-26b2-495a-a872-bc91a4873f18
View f97d9211-26b2-495a-a872-bc91a4873f18
f97d9211-26b2-495a-a872-bc91a4873f18
@kendru
kendru / configure-gcloud-profile.md
Created Aug 27, 2019
Configuring a gcloud profile
View configure-gcloud-profile.md

Configuring a gcloud profile

  1. Create a new profile:
luther:~ ameredith$ gcloud config configurations create my-profile
Created [my-profile].
Activated [my-profile].
  1. Authenticate with Google