Skip to content

Instantly share code, notes, and snippets.


Ben Hoyt benhoyt

View GitHub Profile
benhoyt / countwords.fs
Created Mar 12, 2021
Forth: print frequencies of unique words in stdin, most frequent first
View countwords.fs
200 constant max-line
create line max-line allot \ Buffer for read-line
wordlist constant counts \ Hash table of words to count
variable num-uniques 0 num-uniques !
\ Allocate space for new string and copy bytes, return new string.
: copy-string ( addr u -- addr' u )
dup >r dup allocate throw
dup >r swap move r> r> ;
benhoyt / markdown.diff
Created Oct 7, 2020
Diff to override goldmark's code block output
View markdown.diff
diff --git a/internal/markdown/markdown.go b/internal/markdown/markdown.go
index a729b9f..94d29c1 100644
--- a/internal/markdown/markdown.go
+++ b/internal/markdown/markdown.go
@@ -13,8 +13,11 @@ import (
+ ""
benhoyt / flattenjson.go
Created Jul 7, 2020
Little Go tool to flatten JSON input
View flattenjson.go
// Flatten JSON input
// Example:
// $ echo '{"user":"Ben","ints":[true,false,null],"sub":{"x":1,"y":2}}' | go run flattenjson.go
// _.ints[0] = true
// _.ints[1] = false
// _.ints[2] = null
// _.sub.x = 1
// _.sub.y = 2
benhoyt /
Created Oct 17, 2019
Overview of (parts of) the Python standard library

I'm going to demo a bunch of Python builtin and stdlib functions. There's a lot to get through, so I'll be going fast, but please stop me and ask questions as we go. The goal is to give you a taste of Python's power and expressivity if you're not a Python person, or maybe teach you a few new tricks if you are already.

Built-in functions

# enumerate: iterate with index *and* item
>>> strings = ['123', '0', 'x']
>>> for i, s in enumerate(strings):
...     print(f'{i} - {s}')  # f-strings!
benhoyt / join.awk
Created Nov 20, 2018
AWK program to compare time complexity of joining strings
View join.awk
# AWK program to compare time complexity of joining strings using a
# simple O(N^2) algorithm and a slightly more complex O(N log N) one.
# Join array elements, separated by sep: O(N^2) version
function join1(a, sep, i, s) {
for (i = 1; i+1 in a; i++) {
s = s a[i] sep
if (i in a) {
s = s a[i]
benhoyt / mandelbrot.go
Created Sep 21, 2018
Go program to print the Mandelbrot set on stdout
View mandelbrot.go
// Print the Mandelbrot set on stdout
package main
import (
const (
benhoyt /
Created May 8, 2018
Calculate edit distance with simple (memoized) recursive algorithm
def distance(s, t, cache=None):
"""Return minimum edit distance between s and t, where an edit
is a character substitution, deletion, or addition.
if not s:
return len(t)
if not t:
return len(s)
if cache is None:
benhoyt / connect4.go
Last active Jan 25, 2018
Connect 4 solver
View connect4.go
// A little "Connect Four" game
package main
import (
benhoyt / server.go
Last active Nov 29, 2017
Simple HTTP server with regex-based router in Go
View server.go
// Simple HTTP server with regex-based router
package main
import (
benhoyt / tiny-vdom-speed.html
Created Oct 28, 2017
Speed test of tiny virtual dom and merge algorithm
View tiny-vdom-speed.html
<head><title>More speed tests</title></head>
<h1>More speed tests</h1>
<div id="main"></div>
{% for item in items %}
<li>item: {{ item }}</li>