Skip to content

Instantly share code, notes, and snippets.

Carl Johnson carlmjohnson

Block or report user

Report or block carlmjohnson

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View utils.js
const onAnimate = (eventType, callback) => {
let timeout;
window.addEventListener(eventType, ev => {
if (timeout) {
window.cancelAnimationFrame(timeout);
}
timeout = window.requestAnimationFrame(callback);
});
};
View combinations.go
package main
import (
"fmt"
"strings"
)
func Combinations(n int, combs []int) (next func() bool) {
k := len(combs)
if k == 0 || n < k {
View As() and Is() are useful for optional interfaces beyond error.md

EDIT: On further thought, the implications of using iface.As, e.g., for io.WriterTo are quite subtle. If you were trying, say, to add logging to all Read calls, you wouldn't want to simply pass through to the underlying io.WriterTo because the call would go unlogged. Instead you'd need to implement the As method on your type so as to only return a WriterTo under certain conditions. Still, I think this idea is worth exploring more.


The Go 2 process has produced a proposal for extending the errors standard package, which is currently in testing.

My observation about the experimental errors package is: As() and Is() solve problems that exist well beyond the error interface.

Essentially the problem is this, many packages begin by requiring a certain interface, such as error, io.Reader, or

View html-scan.go
package main
import (
"bufio"
"bytes"
"fmt"
)
const html = `
<html>
@carlmjohnson
carlmjohnson / markov.js
Created Nov 5, 2018
Simple Markov text generator
View markov.js
class Markov {
constructor(text) {
text = text.replace(/\./g, " . ");
text = text.replace(/\s\s+/g, " ");
this.map = {};
let w1 = "";
let w2 = "";
text.split(/\s/).forEach(w3 => {
if (!this.map[w1 + " " + w2]) this.map[w1 + " " + w2] = [];
this.map[w1 + " " + w2].push(w3);
@carlmjohnson
carlmjohnson / raise_chain.py
Last active Dec 19, 2018
How to chain exceptions in Python
View raise_chain.py
# Thing that creates exceptions
def r(n):
raise Exception(f"error: {n}")
# Add exceptions to a list
errors = []
for n in range(10):
try:
r(n)
@carlmjohnson
carlmjohnson / README.md
Created Jun 5, 2018
Go project README template
View README.md

$NAME GoDoc Go Report Card

$DESCRIPTION

Installation

First install Go.

If you just want to install the binary to your current directory and don't care about the source code, run

@carlmjohnson
carlmjohnson / git gud.md
Last active Dec 19, 2018
Git Standards
View git gud.md

Git standards Creative Commons License

Git is the democracy of programming: it is the worst tool for version control, except all those other tools that have been tried from time to time. Among other problems, Git’s command names are obtuse (what is a “rebase”?) and non-orthogonal (how is “resetting” different than “checking out”?), and Git generally impedes the creation of an accurate mental model, but without understanding its underlying directed acyclic graph, one can’t move from beginner to intermediate user. Still, it is a necessary tool in every developer’s toolbox, and following good Git practices leads to smoother, more productive development. This guide assumes you already know how to use Git and discusses some of the higher level issues around standards for collaboration.

 

[<img s

@carlmjohnson
carlmjohnson / paginator.py
Last active Mar 26, 2018
Python for making 1, 2 … 5, 6, 7 … 10, 11 pagination correctly.
View paginator.py
def pages(n, max, window=1, ellipsis=None):
s = set(range(window + 2))
s.update(range(n-window, n+window+1))
s.update(range(max-window, max+1))
seq = [i for i in sorted(s) if i > 0 and i <= max]
res = [1]
for i, nxt in zip(seq, seq[1:]):
diff = nxt - i
if diff == 2:
res.append(i+1)
View net-dsl.go
package main
import (
"fmt"
"log"
"net"
"net/http"
"time"
)
You can’t perform that action at this time.