Skip to content

Instantly share code, notes, and snippets.

@andkerosine
andkerosine / raskell.rb
Created August 15, 2012 05:56
Haskell-like list comprehensions in Ruby
$stack, $draws = [], {}
def method_missing *args
return if args[0][/^to_/]
$stack << args.map { |a| a or $stack.pop }
$draws[$stack.pop(2)[0][0]] = args[1] if args[0] == :<
end
class Array
def +@
cls = "single#{i.zero? ? " first" : i == images.size - 1 ? " last" : ""}"
cls = "single" + {0 => " first", images.size - 1 => " last"}[i]
class Integer
def method_missing meth, *args
super unless meth[/base\d+/]
to_s meth[4,2.0].to_i
end
end
@andkerosine
andkerosine / ws-asm.rb
Created September 9, 2012 16:15
Whitespace assembler in Ruby
ops = %w[push pop dup copy swap slide add sub mul div mod store load label call jump jz jn ret exit ichr inum ochr onum]
quat = [5, 31, 29, 25, 30, 27, 149, 150, 151, 153, 154, 41, 42, 53, 54, 55, 57, 58, 59, 63, 185, 186, 181, 182]
ops = Hash[ops.zip quat.map { |q| q.to_s(4).tr '123', " \t\n" }]
def encode num
(num < 0 ? "\t" : ' ') + ('%b' % num.abs).tr('01', " \t") + "\n"
end
source = File.read ARGV[0]
source.gsub! /\s*[;#].+/, ''
push 3
push 0
dup
inum
load
0:
swap
push 3
mul
push 0
dup
inum
load
dup
push 20
div
0:
dup
@andkerosine
andkerosine / fen.rb
Created August 24, 2012 21:30
FEN Move Validator
def side piece
piece == ' ' ? -1 : piece =~ /[a-z]/
end
fen = File.read ARGV.shift
board = [*'1'..'8'].reverse.product [*'a'..'h']
from, to = *ARGV.map { |a| board.index a.reverse.split // }
raise 'Wat.' if from == to
board, act, _ = fen.split
class Symbol
def | other
-> arg { arg.send(self).send(other) }
end
end
class Proc
def | other
-> arg { call(arg).send(other) }
end