Skip to content

Instantly share code, notes, and snippets.

View vysogot's full-sized avatar
🎯
Ruby/Redis/SQL

Jakub Godawa vysogot

🎯
Ruby/Redis/SQL
View GitHub Profile
@vysogot
vysogot / permset.rb
Created July 4, 2011 18:12
permute sets with acumulators
def permute(*args)
# initialize the register for each array
# with [array itself, offset, its size]
memory = []
perm_count = 1
args.each do |array|
size = array.size
memory << [array, 0, size-1]
@vysogot
vysogot / croissant.pl
Created July 5, 2011 09:01
croissant equation in eclipse clp
:- lib(ic).
croissant(X,Y,Z) :-
(X^2+Y^2+Z^2+7*sqrt(5)/2-11/2)^2 - ((1+sqrt(5))*X-7+3*sqrt(5))^2-(1+sqrt(5))^2*Y^2 $= 0,
locate([X,Y,Z], 1e-5).
# one of the solutions
croissant(X,Y,Z).
X = X{0.0 .. 7.4073630564431975e-6}
Y = Y{1.066573563196598 .. 1.0665754743970506}
@vysogot
vysogot / croissant.rb
Created July 5, 2011 09:02
croissant ordering
bar.receive_order(client.put_words_in_mouth('Un croissant, por favor.').speak_up)
@vysogot
vysogot / test_user_story.rb
Created July 5, 2011 09:05
user stories for test unit
module Test::Unit::UserStory
class UserStoryTestCase < ActionController::IntegrationTest
class << self
attr_accessor :description
alias :it :test
def criteria(name, options = {}, &block)
klass = create_class(name, self, &block)
helper_klass = options[:js] ? CapybaraJavascriptTestHelper : CapybaraTestHelper
@vysogot
vysogot / example_user_story_test.rb
Created July 5, 2011 09:07
example test user story
require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
include Test::Unit::UserStory
story "Create the thing", %{
As a user
I want to create something
In order to show something
} do
criteria "without js when logged in" do
@vysogot
vysogot / fast_permute_sets.rb
Created July 7, 2011 15:49
Fast, fully iterative permute sets
def permute(*args)
# filter the arguments
args.reject! {|x| !x.is_a?(Array) || x.empty?}
# get the number of all combinations
total = args.inject(1) {|total, array| total * array.size}
# prepare the small_cycles array to know how many times
# one element should be repeated in a loop
@vysogot
vysogot / fast_cartesian_product.rb
Created July 7, 2011 18:48
Cartesian product
def cartprod(*args)
result = [[]]
while [] != args
t, result = result, []
b, *args = args
t.each do |a|
b.each do |n|
result << a + [n]
end
end
@vysogot
vysogot / filter_matcher_test.rb
Created July 16, 2011 20:17
FilterMatcher test suite
require 'test/unit'
class PeopleMatcherTest < Test::Unit::TestCase
def setup
@db = [
{ :id => 1, :name => "John", :age => 33, :homepage => "www.johny.com", :matched => false },
{ :id => 2, :name => "Mike", :age => 30, :homepage => "www.mikes.com", :matched => false },
{ :id => 3, :name => "Johny", :age => 25, :homepage => "www.johny.com", :matched => false },
{ :id => 4, :name => "Mike", :age => 30, :homepage => "www.realmike.com", :matched => false },
{ :id => 5, :name => "Dan", :age => 25, :homepage => "www.danny.com", :matched => false },
@vysogot
vysogot / filter_matcher.rb
Created July 16, 2011 20:26
FilterMatcher module
module FilterMatcher
#
# define filter in a class that uses this module
# named like:
# - name_filter
# - age_filter
#
# they should a filtered array
#
@vysogot
vysogot / people_matcher.rb
Created July 16, 2011 20:27
Using FilterMatcher example
class PeopleMatcher
include FilterMatcher
attr_reader :db
def initialize(db, input)
@db, @input = db, input
end
def match