Skip to content

Instantly share code, notes, and snippets.

@jamis
jamis / maze.hs
Last active Aug 29, 2015
An implementation of the Recursive Backtracker maze generation algorithm in Haskell
View maze.hs
{- --------------------------------------------------------------------------
- A naive random maze generator.
-
- $ ghc -o maze Maze.hs
- $ maze 10 10
-
- Author: Jamis Buck (jamis@jamisbuck.org)
- ------------------------------------------------------------------------ -}
module Main where
@jamis
jamis / maze.erl
Created Aug 17, 2015
An implementation of the Recursive Backtracker maze generation algorithm in Erlang
View maze.erl
-module(maze).
-export([generate/2,north/3,south/3,west/3,east/3,visualize/1]).
% Generate and return a maze of width W and height H.
generate(W,H) -> try_directions(random:uniform(W)-1, random:uniform(H)-1, directions(), {width,W,height,H,0}).
% Returns true if there is a passage north from the given position in the maze.
north(X,Y,Maze) -> at(X,Y,Maze) band 2#1 =/= 0.
% Returns true if there is a passage south from the given position in the maze.
@jamis
jamis / grid.txt
Created Aug 4, 2015
A program for solving Sam Lloyd's "Back from the Klondike" puzzle.
View grid.txt
__________xxx__________
_______xxx477xxx_______
_____xx544833463xx_____
____x1451114517135x____
___x494967555876685x___
__x37298356739187585x__
__x14784292711822763x__
_x7218553113133428613x_
_x4267252422543281773x_
_x4165111914344319827x_
@jamis
jamis / query-parser.rb
Last active Oct 1, 2019
A simple example of a BNF-based, recursive-descent parser for reading a field-based query string
View query-parser.rb
require 'strscan'
# expr := term
# | term AND expr
# | term OR expr
# term := value
# | atom ':' value
# atom := word+
# | quoted_string
# value := atom
@jamis
jamis / keybase.md
Created Sep 23, 2014
Verifying my GitHub identity with Keybase.io
View keybase.md

Keybase proof

I hereby claim:

  • I am jamis on github.
  • I am jamis (https://keybase.io/jamis) on keybase.
  • I have a public key whose fingerprint is D6BF 2C78 D257 1F45 892A F160 3E22 BCB7 321F F098

To claim this, I am signing this object:

@jamis
jamis / assets_resource.rb
Created Oct 22, 2011
Sprockets asset resource for Webmachine (asset pipelining!)
View assets_resource.rb
class Resources::Assets < Webmachine::Resource
def content_types_provided
@asset = Application.assets[request.uri.path]
if @asset.present?
[[@asset.content_type, :to_asset]]
else
accept = request.headers['Accept'] || "text/html"
[[accept.split(/,/).first.split(/;/).first.strip, :to_html]]
end
@jamis
jamis / string_byteSize.js
Created Oct 19, 2011
Compute how many bytes you'd need to represent a Javascript string in UTF-8. This is great if you're doing naughty things like forcing utf-8 encoded text into latin1 database columns, and want to avoid silently truncating text.
View string_byteSize.js
/* Returns the number of bytes needed to represent the given
* Javascript string in UTF-8. */
String.prototype.byteSize = function () {
var bytes = 0;
for(i = 0; i < this.length; i++) {
var charCode = this.charCodeAt(i);
if (charCode <= 0x7F)
bytes += 1;
else if (charCode <= 0x7FF)
bytes += 2;
@jamis
jamis / mysql2-benchmarks.rb
Created Aug 9, 2011
mysql vs. mysql2 benchmarks
View mysql2-benchmarks.rb
require 'optparse'
module WordPicker
def pick
self[rand(length)]
end
LENGTHS = [1] + [2]*3 + [3]*4 + [4]*4 + [5]*4 + [6]*3 + [7]*2 + [8]
def sentence
words = []
@jamis
jamis / demo.rb
Created Aug 3, 2011
Repairing a unicode string that contains invalid characters
View demo.rb
# encoding: utf-8
s = "Blah \xe9 blah 헌글"
puts "BEFORE"
puts "encoding: #{s.encoding}"
puts "valid : #{s.valid_encoding?}"
puts "text : #{s}"
s = s.
@jamis
jamis / cap-transcriber.rb
Created Jul 25, 2011
Capistrano Transcriber: a wrapper script for Capistrano that writes session transcripts to a timestamped file.
View cap-transcriber.rb
#!/usr/bin/env ruby
require 'capistrano/cli'
class TranscriptLogger
module TranscriptLoggerMetadata
attr_accessor :tx_logger_metadata
def self.apply_to(object, options={})
object.extend(self) unless object.respond_to?(:tx_logger_metadata)