Skip to content

Instantly share code, notes, and snippets.

@choplin
choplin / projecteuler1.sql
Created May 19, 2011 12:38
Solution of Project Euler #1 with PostgreSQL
SELECT
sum(num)
FROM
generate_series(1, 999) AS t(num)
WHERE
(num % 3) = 0
OR (num % 5) = 0
;
CREATE TABLE t (
id int
,p cube
);
CREATE INDEX _idx_p ON t USING gist (p);
INSERT INTO t VALUES (1, '(1,2,3)');
EXPLAIN SELECT * FROM t WHERE p <@ '(0,0,0),(3,3,3)';
DROP TABLE IF EXISTS t;
CREATE TABLE t (
id int PRIMARY KEY
,adj int[]
);
CREATE INDEX _idx_adj on t USING gin (adj);
INSERT INTO t VALUES
(1, ARRAY[2,3,4,5])
@choplin
choplin / mongo-sample.html
Created June 11, 2011 08:51
MongoDB JP 第4回勉強会のサンプルコード
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
checkTweet();
$('#input').submit(function(){
var name = $('#name').val();
var tweet = $('#tweet').val();
@choplin
choplin / quickrun_config.vim
Created July 7, 2011 16:44
vim-quickrun config for psql
"quickrun_config for psql
unlet! g:quickrun_config
let g:quickrun_config = {
\ 'sql':{
\ 'command': 'psql',
\ 'cmdopt': '-d postgres',
\ 'exec': '%c %o -f %s',
\ },
\}
@choplin
choplin / sieve_of_eratosthenes.sql
Created July 8, 2011 17:37
Implementation of Sieve of Eratosthenes by SQL(PostgreSQL)
CREATE OR REPLACE FUNCTION sieve_of_eratosthenes(int) RETURNS SETOF int AS $$
WITH RECURSIVE
--数列を用意
t1(n) AS (
SELECT generate_series(2, $1)
),
t2 (n, i) AS (
--初期化:2で割り切れない集合を作成
SELECT
n
@choplin
choplin / surroud_fold_marker.vim
Created July 18, 2011 18:20
using surround.vim custom mapping (surrounding with fold marker)
let g:surround_custom_mapping.vim= {
\'z': "\"{{{ \r \"}}}"
\ }
@choplin
choplin / gist:1261347
Created October 4, 2011 10:54
how to use _addSpecial command via c++ driver
BSONObjBuilder b;
b.append( "query", BSONObj() ); // append original BSON object of query
b.append( "$maxScan", 1 ); // append special parameter
auto_ptr<DBClientCursor> cursor = c.query( ns, Query(b.obj()) );
while( cursor->more() ){
BSONObj p = cursor->next();
cout << p.toString() << endl;
}
DROP TABLE IF EXISTS t;
CREATE TABLE t (
id int PRIMARY KEY
,adj int[]
);
INSERT INTO t VALUES
(1, ARRAY[2,3,4,5])
,(2, ARRAY[1])
@choplin
choplin / vim-vcs-api-log
Created October 30, 2011 06:44
vcs.vim api-log plugin
let s:save_cpo = &cpo
set cpo&vim
let s:api = {
\ 'name': 'log',
\ }
function! s:api.depends()
return ['log']
endfunction