Skip to content

Instantly share code, notes, and snippets.

View carlism's full-sized avatar

Carl Leiby carlism

View GitHub Profile
defmodule AdventOfCode.Day02 do
@loss 0
@draw 3
@win 6
@rock 1
@paper 2
@scisors 3
def cons(x,y)
->(m){ m.call(x, y) }
end
def car(z)
z.call( ->(p, q) {p} )
end
def cdr(z)
z.call( ->(p, q) {q} )
@carlism
carlism / redis_test.rb
Created March 11, 2013 14:33
Sample Ruby interaction with redis and my zwave bridge
require 'redis'
r = Redis.new
nodes = r.keys "zw_node:*"
nodes.each do |node|
name = r.hget node, "nodeName"
value = r.hget node, "v_Basic"
puts "Node: #{name} has value: #{value}"
end
@carlism
carlism / neighborhood.rb
Created September 28, 2012 16:24
Game Of Life Neighborhood
def neighborhood
n = []
[-1,0,1].each {|y_adj|
[-1,0,1].each {|x_adj|
n << [@x + x_adj, @y + y_adj]
}
}
n.delete_at(4)
n
require 'ostruct'
class Article < OpenStruct
def initialize(hash=nil, &blk)
super(hash)
instance_eval &blk if blk
end
def method_missing(method, *args)
super "#{method}=".to_sym, args
@carlism
carlism / parse.rb
Created January 18, 2011 02:10
small parsing exercise
def parse(name)
results = []
tokens = name.split(/(\[[^\]]*\])/).reject{|s| s.empty? }
permutations = 2**tokens.count{|s| s.start_with?("[") }
permutations.times do |permutation|
result = ""
opt_count = 1
tokens.each do |word|
if word.start_with?("[")
if (opt_count & permutation) > 0
require 'java'
require 'jmatharray.jar'
id = org.math.array.LinearAlgebra.identity(4)
ruby_arr = [[0.25, 0.25, 0.25, 0.25],
[0.25, 0.25, 0.25, 0.25],
[0.25, 0.25, 0.25, 0.25],
[0.25, 0.25, 0.25, 0.25]]
(defn build [size operation]
(if (== size 1) (list (eval operation))
(concat (list (eval operation)) (build (dec size) operation))
)
)
(println (build 25 '(int (rand 2048))))
(println (build 2 '(int (rand 20))))
private ArrayList<Integer> d = new ArrayList<Integer>();
@Test
public void testSort() {
this.d.add(5);
this.d.add(10);
this.d.add(1);
Collections.sort(this.d); // -&gt; I&apos;m getting a type mismatch here
assertEquals(1, this.d.get(0).intValue());
assertEquals(5, this.d.get(1).intValue());