Skip to content

Instantly share code, notes, and snippets.

(defstruct chopstick :id :holder)
(defstruct philosopher :name :left-stick :right-stick)
(defn make-chopstick [id]
(struct chopstick id (ref "")))
(defn set-table [philosophers]
(let [stick-count (max 2 (count philosophers))
stick-seq (map make-chopstick (range stick-count))
stick-cycle (cycle (take stick-count stick-seq))]
(defn nth-val [first-val step val-count]
(+ first-val (* step (dec val-count))))
(defn arithmetic-series [first-val step val-count]
(let [last-val (nth-val first-val step val-count)]
(* val-count
(/ (+ first-val last-val) 2))))
(defn arithmetic-series-of-multiples-less-than [ceiling]
(fn [val]
(let [fibs (iterate (fn [[a b]] [b (+ a b)]) [0 1])
just-the-fibs (take-while #(> 4e6 (apply max %)) fibs)]
(reduce (fn [sum [_ r]] (+ sum (if (even? r) r 0)))
0
just-the-fibs))
class Foo
attr_reader :name
def initialize(name)
@name = name
end
def method_missing(name, *args, &block)
return super unless name.to_s =~ /^go_/
puts "Defining #{name}"
method = lambda { puts "hey #{@name}@#{object_id}, #{name.to_s.tr '_', ' '}" }
@foogoof
foogoof / rspec should be readable.rb
Created October 16, 2010 05:56
Code sample written for a 5 minute talk on RSpec
describe "NIC-U" do
context "Fall conference" do
it "should be readable" do
"readable".should == "readable"
end
it { "readable".should == "readable"}
context "readable" do
it { should == "readable" }
@foogoof
foogoof / gist:950930
Created May 1, 2011 22:13
world, hello in node.js
function hello_world () {
setTimeout(function() {
console.log("world");
}, 5000);
console.log("hello");
}
hello_world();
@foogoof
foogoof / vows_unit_tests_for_ansi_code.js
Created May 2, 2011 12:30
test code from ansi control character code
var batch_util = {
'utility functions': {
'starts_sequence' : {
'can detect a simple sequence': function() {
assert.ok(ansi.util.starts_sequence('\u001b[foobar'));
},
'rejects something obviously not a sequence': function() {
assert.ok(!ansi.util.starts_sequence('foobar'));
},
'rejects a truncated sequence': function() {
@foogoof
foogoof / recall_this_callback.js
Created May 8, 2011 21:55
trying to verify a sequence of messages in one vows.js test
// vows calls transmute, which registers trap as the listener to all specified events
// node sends each message to the trap() callback
// FAIL: trap calls this.callback with its arguments
// PASS: trap calls this.callback with a property which cached arguments from each invocation of trap
// HOWTO pass / fail: toggle the conditional at line 152
// revised sample output:
//
// PASS
//
@foogoof
foogoof / index.html
Created May 18, 2011 12:28
html page to tail system.log over websockets
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = new io.Socket();
socket.connect();
socket.on('connect',
function(){
console.log('connected');
socket.send('/var/log/system.log');
});
@foogoof
foogoof / webtail.js
Created May 18, 2011 12:29
Express / node / socket.io code to stream unix child process output over websockets
//
// Just skip to the bottom
//
var express = require('express');
var io = require('socket.io');
var app = module.exports = express.createServer();
// Configuration