Skip to content

Instantly share code, notes, and snippets.

@PJK
PJK / Example.cs
Created February 3, 2011 21:15
DESF usage exaple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DESF;
using DESF.Flow.Event;
using DESF.Flow.Calendar;
using DESF.Tools;
namespace CalendarTest
@PJK
PJK / BasePresenter.php
Created April 14, 2011 10:33
TwigMacro
<?php
public function templatePrepareFilters($template) {
$template->registerFilter($latte = new Nette\Templates\LatteFilter());
TwigMacro::register($latte->getHandler());
}
@PJK
PJK / cache.php
Created April 27, 2011 11:41
Reference caching test
<?php
require("class.php");
$cache = $a->a->a;
for ($i=1; $i<=$loopcount; $i++) {
$cache->a;
}
<?php
//Horner, hovada.....
function decToBin($num) {
do {
$res[] = $rem = $num % 2;
$num = floor($num/2);
} while ($num);
return join("",array_reverse($res));
}
/**
* @author Pavel Kalvoda
*/
abstract class BasePresenter extends Nette\Application\Presenter
{
<?php
/**
* @author Pavel Kalvoda
*/
abstract class BasePresenter extends Nette\Application\Presenter {
protected function getAutosaveableProperties() {
$props = $this->getReflection()->getProperties();
$props = array_filter($props, function($prop) {
@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(
@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 / 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: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