Skip to content

Instantly share code, notes, and snippets.

@PJK
PJK / bridge.rb
Created March 11, 2015 16:33
Bridge pattern
class Window
def initialize
# Implementor selection strategy
extend [WindowsAPI, X11API].sample
end
def drawRectangle
4.times { drawLine }
end
end
module M
def x
'M'
end
end
class A
def x
42
end
package com.pavelkalvoda.misc.smoothvox.terrain;
import java.util.Random;
// Based on http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
// http://www.csee.umbc.edu/~olano/s2002c36/ch02.pdf
// http://mrl.nyu.edu/~perlin/doc/oscar.html
// + simplified
public class SimpleSimplexNoise {
vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:271:in `call': undefined method `url_options' for #<Module:0x007fd2d8597d58> (NoMethodError)
from vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:222:in `call'
from vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:334:in `block (2 levels) in define_url_helper'
from config/environment.rb:134:in `block (2 levels) in <top (required)>'
[... app specific code, sorry ...]
from vendor/bundle/ruby/2.0.0/gems/sidekiq-3.2.5/lib/sidekiq/processor.rb:52:in `block (2 levels) in process'
from vendor/bundle/ruby/2.0.0/gems/sidekiq-3.2.5/lib/sidekiq/middleware/chain.rb:122:in `call'
from vendor/bundle/ruby/2.0.0/gems/sidekiq-3.2.5/lib/sidekiq/middleware/chain.rb:122:in `block in invoke'
from vendor/bundle/ruby/2.0.0/gems/sidekiq-pro-1.9.0/lib/sidekiq/batch/middleware.rb:26:in `call'
from vendor/bundle/ruby/2.0.0/gems/sidekiq-3.2.5/lib/sidekiq/middlewar
require 'rubygems'
require 'bundler'
Bundler.require
include Contracts
Contract Num, Num => Num
def add_c(a, b)
a + b
end
@PJK
PJK / gist:4164101
Created November 28, 2012 20:28
RGB sorting
#!/usr/bin/env ruby
t = [:r, :g, :b]
puts (inp = ARGV[0].split("").map {|x| x.downcase.to_sym}).inspect
cc = Hash[t.map {|c| [c,inp.count(c)]}]
expec = t.map {|c| [c]*cc[c]}.flatten
puts (slc = Hash[t.zip([(0...cc[:r]), (cc[:r]...cc[:r]+cc[:g]), (cc[:r]+cc[:g]...cc[:r]+cc[:g]+cc[:b])])]).inspect
inp.each_with_index do |c, i|
next if expec[i] == c #ok
if inp[slc[c]].index(expec[i]) then # fix 2 positions
@PJK
PJK / gist:1340274
Created November 4, 2011 19:36
Finding primes < 1,000
pjk@desktop:~/NetBeansProjects/Primes$ time java -jar "/home/pjk/NetBeansProjects/Primes/dist/Primes.jar"
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
real 0m0.083s
user 0m0.064s
sys 0m0.012s
@PJK
PJK / gist:1340007
Created November 4, 2011 17:54
Ruby quicksort
#!/usr/bin/env ruby
data = Array.new
50.times { data << rand(100) }
def qs(list)
return list if list.length < 2
smaller, bigger = list[1..-1].partition {|x| x < list[0] }
return qs(smaller) + [list[0]] + qs(bigger)
end
@PJK
PJK / Services.rb
Created May 30, 2011 15:23
Sinatra validation autowiring
module Helpers
# Only a few dragons here. This method validates body of the request
# against a schema. If no schema was passed to the method, it will
# try to find it automagically
def validate!(schema = nil)
schema ||= Kernel.const_get(
settings.validation[:module]
).const_get(
/^\/(.*)\//.match(request.path)[1].capitalize
).const_get(
<?php
/**
* @author Pavel Kalvoda
*/
abstract class BasePresenter extends Nette\Application\Presenter {
protected function getAutosaveableProperties() {
$props = $this->getReflection()->getProperties();
$props = array_filter($props, function($prop) {