Skip to content

Instantly share code, notes, and snippets.

View yuanchuan's full-sized avatar

Yuan Chuan yuanchuan

View GitHub Profile
@yuanchuan
yuanchuan / bad.js
Created December 28, 2011 03:37
get spark list
//bad
var sequence = (function(data, temp){
var len = data.length - 1;
for (var i = 0; i < len; ++i) {
temp.push(data[i+1] - data[i]);
}
return temp.join(',');
})((function(all, buf, user){
var len = all.length;
/**
* 估算有多少人投票
*/
Math.round([].slice.call(
document.querySelectorAll(
'tbody td[bgcolor="#ffffff"]:nth-child(2)'
), 1)
.map(function(n){
return parseInt(
n.innerHTML.split(':(')[1]
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
@yuanchuan
yuanchuan / gist:2178390
Created March 24, 2012 04:51
The ring data structure
var Ring = (function() {
function Ring(list) {
this._list = [].slice.call(list, 0);
this._length = this._list.length;
this._curr = -1;
};
Ring.prototype = {
rollTo: function(idx) {
this._curr = idx % this._length;
if (this._curr < 0) this._curr += this._length;
@yuanchuan
yuanchuan / gist:2586540
Created May 3, 2012 15:33
Watch a directory recursively
/**
* Watch a directory recursively
*
* @param {String} dir
* @param {Function} cb
*
* watchRecursive('directory', function(current) {
* console.log(current, ' changed');
* });
*/
@yuanchuan
yuanchuan / gist:2653472
Created May 10, 2012 14:39
in favor of nodejs
#!/usr/bin/env node
var fs = require('fs')
, path = require('path')
, exec = require('child_process').exec
, src_css = '../css/'
, target_css = '../../css/'
, src_js = '../js/'
, target_js = '../../js/'
, compiler = 'tools/compiler.jar';
@yuanchuan
yuanchuan / gist:2760768
Created May 21, 2012 06:20
filter log file with nodejs for fun
#!/usr/bin/env node
var fs = require('fs')
, input = 'worklog.in'
, output = 'worklog.out';
var spliters = {
block: 'Revision'
, field: [
@yuanchuan
yuanchuan / gist:2761842
Created May 21, 2012 11:08
Quick and dirty way to check if a website is gzipped
#!/usr/bin/env node
/**
* Quick and dirty way to check if a website is gzipped
* Usage:
* ./isgzipped www.xxx.com
*/
var exec = require('child_process').exec
, site = process.argv[2];
@yuanchuan
yuanchuan / one day later.hs
Created June 12, 2012 06:19
Learning Haskell
-- 第一印象:使用 Haskell 计算排列组合这样轻而易举。
[ [a,b,c] | a <- [1..10], b <- [1..10], c <- [1..10] ]
[ [a,b,c] | a <- [1..10], b <- [1..a], c <- [1..b] ]
[ [a,b,c] | a <- "abc", b <- "abc", c <- "abc" ]
@yuanchuan
yuanchuan / gist:3232510
Created August 2, 2012 02:12
download pages with nodejs
var fs = require('fs')
, http = require('http')
, path = require('path')
, cheerio = require('cheerio');
var archive = 'http://apod.nasa.gov/apod/archivepix.html'
, base = path.dirname(archive) + '/'
, target = 'archive/english/';