Skip to content

Instantly share code, notes, and snippets.

View le0pard's full-sized avatar
:octocat:
Stupid projects require stupid solutions

Oleksii Vasyliev le0pard

:octocat:
Stupid projects require stupid solutions
View GitHub Profile
@le0pard
le0pard / gist:910763
Created April 8, 2011 21:22
Random Range
CREATE OR REPLACE FUNCTION random(numeric, numeric)
RETURNS numeric AS
$$
SELECT ($1 + ($2 - $1) * random())::numeric;
$$ LANGUAGE 'sql' VOLATILE;
@le0pard
le0pard / gist:910793
Created April 8, 2011 21:32
Luhn algorithm
CREATE OR REPLACE FUNCTION luhn_verify(int8) RETURNS BOOLEAN AS $$
-- Take the sum of the
-- doubled digits and the even-numbered undoubled digits, and see if
-- the sum is evenly divisible by zero.
SELECT
-- Doubled digits might in turn be two digits. In that case,
-- we must add each digit individually rather than adding the
-- doubled digit value to the sum. Ie if the original digit was
-- `6' the doubled result was `12' and we must add `1+2' to the
-- sum rather than `12'.
@le0pard
le0pard / gist:972335
Created May 14, 2011 15:45
Куайн, Запрос который выводит сам себя
select a || ' from (select ' || quote_literal(a) || b || ', ' || quote_literal(b) || '::text as b) as quine' from (select 'select a || '' from (select '' || quote_literal(a) || b || '', '' || quote_literal(b) || ''::text as b) as quine'''::text as a, '::text as a'::text as b) as quine;
@le0pard
le0pard / gist:972713
Created May 14, 2011 22:38
Ускоряем LIKE
prefix_test=# create table tags (
prefix_test(# tag text primary key,
prefix_test(# name text not null,
prefix_test(# shortname text,
prefix_test(# status char default 'S',
prefix_test(#
prefix_test(# check( status in ('S', 'R') )
prefix_test(# );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tags_pkey" for table "tags"
CREATE TABLE
@le0pard
le0pard / gist:1058405
Created July 1, 2011 12:03
Skypekit ruby Makefile
SHELL = /bin/sh
#### Start of system configuration section. ####
srcdir = ../../../../ext/skypekit4ruby
topdir = /home/leo/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1
hdrdir = /home/leo/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1
arch_hdrdir = /home/leo/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1/$(arch)
VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
@le0pard
le0pard / controller.rb
Created August 15, 2011 12:41
Standart ActionController and ActionController::Metal comparison for api (rails 3.1rc5)
class V1Controller < ApplicationController
before_filter :check_some
def index
response_hash = {:a => [1,2,3], :status => "OK"}
respond_to do |format|
format.xml { render :xml => response_hash.to_xml(:root => "response", :dasherize => false, :skip_types => true) }
format.json { render :json => response_hash, :callback => params[:callback] }
end
@le0pard
le0pard / backbone_sync_hack.coffee
Created September 10, 2011 17:35 — forked from jumski/backbone_sync_hack.coffee
BackboneJS sync hack to namespace params when saving/updating model
###
Usage:
* add model.name property that will be used as a namespace in the json request
* put this code before your Backbone app code
* use toJSON() as usual (so there is no namespacing in your templates)
* your model's data will be sent under model.name key when calling save()
###
# save reference to Backbone.sync
Backbone.oldSync = Backbone.sync
@le0pard
le0pard / gist:1234632
Created September 22, 2011 12:09
rails 3.1 initializable list
handle_lib_autoload: 0.000 sec
set_load_path: 0.001 sec
set_load_path: 0.001 sec
set_load_path: 0.001 sec
set_load_path: 0.000 sec
set_load_path: 0.001 sec
set_autoload_paths: 0.000 sec
set_autoload_paths: 0.000 sec
set_autoload_paths: 0.000 sec
set_autoload_paths: 0.000 sec
@le0pard
le0pard / gist:1571839
Created January 6, 2012 18:40
Erlang factorial
-module(recursive).
-export([fac/1, tail_fac/1]).
fac(N) when N == 0 -> 1;
fac(N) when N > 0 -> N*fac(N-1).
tail_fac(N) -> tail_fac(N,1).
tail_fac(0,Acc) -> Acc;
tail_fac(N,Acc) when N > 0 -> tail_fac(N-1,N*Acc).
@le0pard
le0pard / 0-readme.md
Created February 21, 2012 21:58 — forked from burke/0-readme.md
ruby-1.9.3-p125 cumulative performance patch.

Patched ruby 1.9.3-p0 for 30% faster rails boot

What is?

This script installs a patched version of ruby 1.9.3-p0 with patches to make ruby-debug work again (#47) and boot-time performance improvements (#66 and #68), and runtime performance improvements (#83 and #84).

Huge thanks to funny-falcon for the performance patches.