Skip to content

Instantly share code, notes, and snippets.

@vidarh
vidarh / closures-basic.c
Created December 18, 2009 12:10
A number of ways to implement closures in C, in preparation of an upcoming blog post
#include <stdio.h>
#include <stdlib.h>
struct closure {
void (* call)(struct closure *);
int x;
};
@vidarh
vidarh / gviz.rb
Created September 24, 2013 00:51
Rack middleware for inline Graphviz graphs
require 'digest'
module GViz
CACHE_PATH = "/tmp/gviz/"
ENGINES = %w{dot neato twopi circo fdp sfdp}
XSLTPROC = `which xsltproc`.chomp
notugly = File.dirname(__FILE__)+"/notugly.xsl"
NOTUGLY = (File.exists?(notugly) && XSLTPROC != "") ? notugly : nil
@vidarh
vidarh / gist:3360589699389db75ea3
Created March 19, 2015 10:09
Trivially primitive simulation of survival (or not) of lineages
class Male
@@lineage = 1
def initialize
@lineage = @@lineage
@@lineage += 1
end
def inspect; "<#{@lineage}>"; end
attr_reader :lineage
@vidarh
vidarh / README.md
Created August 15, 2015 23:53
Trivial Go Init

Inspired by http://git.suckless.org/sinit

Provides basic reaping of child processes for use in e.g. containers. Note however that this code compiles to a substantially larger binary than sinit. Sinit linked statically against Musl is <14KB on my machine, while this code ends up at 2.7MB due to the Go runtime.

If you must have a license, consider it under the MIT/X Consortium license (though I'm happy to confirm relicensing to whatever license works for you).

@vidarh
vidarh / Dockerfile-bindtest
Created January 16, 2016 09:03
Testing resolv.conf options
FROM debian
RUN apt-get update && apt-get -y install bind9
EXPOSE 53
EXPOSE 53/udp
CMD ["/usr/sbin/named", "-f"]
@vidarh
vidarh / README.md
Last active July 9, 2017 16:25
Stupid / simple JSON command line processing with Ruby

Based on an example for Elvish.

From comment posted to HN:

I love the structured pipes, but as mentioned in another discussion, replacing the shell is a big leap for a lot of people. And you can get quite far without it with tools like "jq". And when I saw this, I just had to tinker a bit to see what I could do with Ruby, based on the example on the homepage:

$ curl -s https://api.github.com/repos/elves/elvish/issues | jr 'each{|issue| puts "#{issue["number"]}: #{issue["title"]}"} ' | head -n 11

Or (I expect pitchforks when you see the implementation for this):

@vidarh
vidarh / shunting.rb
Created March 18, 2018 15:26
Variant of code from article on operator precedence parser from http://hokstad.com/operator-precedence-parser
require 'pp'
Oper = Struct.new(:pri,:sym,:type)
class TreeOutput
def initialize
@vstack = []
end
def oper o
@vidarh
vidarh / main.rb
Created March 28, 2020 11:42
Simple demo of gravity in Dragonruby
# Click the Run game! button to execute the code.
def vector_add(v1,v2)
v1.zip(v2).map{|p1,p2| p1+p2}
end
# For demonstration purposes, we just blanket apply a terminal velocity.
# Set this low to see the effect.
def resistance(v, gravity)
# This only applies air resistance downwards, which is lazy
@vidarh
vidarh / loader.rb
Created November 10, 2021 15:05
Prototype loader for GtkSourceView themes into Rouge
#
# # Theme loader
#
# Tries to load different types of themes
#
require 'nokogiri'
module ThemeLoader
include Rouge::Token::Tokens
@vidarh
vidarh / sexp.rb
Created June 8, 2023 15:17
Minimal Ruby S-expression parser w/test case showing using %(string syntax) to naturally embed s-expressions in Ruby code.
require 'strscan'
class Sexp
def initialize(str); @s = StringScanner.new(str); end
def s(e); @s.scan(e); end
def r(e); s(e) or raise "Expected #{x}"; end
def list; s("(")&&b=body and r(")") and b; end
def exp; s(/[a-zA-Z][\w_-]*/)&.to_sym||s(/[0-9]+/)&.to_i||s(/"[^"]*"/)&.[](1..-2)||list||r("exp"); end
def body; r=[]; while s(/\s*/) and !@s.eos? and !@s.check(")") and e=exp; r<<e; end; r; end
end
def sexp str; Sexp.new(str).body; end