Skip to content

Instantly share code, notes, and snippets.

View avdi's full-sized avatar

Avdi Grimm avdi

View GitHub Profile
require "thread"
class BoundedQueue
def initialize(max_size = :infinite)
@lock = Mutex.new
@items = []
@item_available = ConditionVariable.new
@max_size = max_size
@space_available = ConditionVariable.new
end
@avdi
avdi / input.xhtml
Created August 20, 2013 07:02
Pandoc converts HTML tables to paragraphs
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>table test</title>
</head>
<body>
<h1>table test</h1>
<table>
<colgroup>
Open3.pipeline_r(
%W[xmllint --xinclude --xmlout #{spine_file}],
# In order to clean up extraneous namespace declarations we need a second
# xmllint process
%W[xmllint --format --nsclean --xmlout -]) do |output, wait_thr|
open(codex_file, 'w') do |f|
IO.copy_stream(output, f)
end
end
@avdi
avdi / macro_modules.rb
Last active August 29, 2018 07:43
Modules for macros. In reference to: http://thepugautomatic.com/2013/07/dsom/
class SomeORM
def self.attributes(*names)
if const_defined?(:DynamicAttributes, false)
mod = const_get(:DynamicAttributes)
else
mod = const_set(:DynamicAttributes, Module.new)
include mod
end
mod.module_eval do
names.each do |name|
@avdi
avdi / def.exs
Last active December 20, 2015 08:19
def next_state
(board, x, y) ->
cell = cell_at(board, x, y)
live_count = live_neighbors(board, x, y)
next_state(cell, live_count)
end
("o", live_count) when live_count in 2..3 -> "o"
("o", _) -> "."
(".", live_count) when live_count === 3 -> "o"
(".", _) -> "."
@avdi
avdi / fib.rb
Created July 15, 2013 04:08
Fibonacci example from "Programming Elixir" translated to Ruby
require 'thread'
require 'benchmark'
module FibSolver
def self.fib(scheduler_queue, my_queue)
loop do
scheduler_queue << [:ready, my_queue]
message, *args = my_queue.pop
case message
when :fib
@avdi
avdi / gol.exs
Last active June 3, 2016 04:30
Game of life in Elixir (take 1)
defmodule Life do
def run(board) when is_binary(board) do
board |> parse_board |> run
end
def run(board) do
IO.write("\e[H\e[2J")
Life.print_board board
:timer.sleep 1000
board = next_board(board)
module Node
def inspect
"<<#{self}>>"
end
end
Number = Struct.new(:value) do
include Node
def to_s
@avdi
avdi / message.rb
Created June 15, 2013 16:17
Something I've been thinking about for a while...
# An experiment in higher-order (?) messages in Ruby.
class Message
attr_reader :name
attr_reader :arguments
def initialize(name, *arguments)
@name = name
@arguments = arguments
end