Skip to content

Instantly share code, notes, and snippets.

@xuyang2
xuyang2 / comb.coffee
Last active August 29, 2015 13:57
Combination generator ported from Java
'use strict'
_ = require 'underscore'
# CombinationGenerator ported from Java.
# The algorithm is described by Kenneth H. Rosen, Discrete Mathematics and
# Its Applications, 2nd edition (NY: McGraw-Hill, 1991), pp. 284-286.
#
# Simple usage:
# combinations(["a", "b", "c", "d", "e"], 3)
(function() {
this.tmpl3 = function tmpl(str, data) {
var value = "var out = ''; out+=" + "'" +
str.replace(/[\r\t\n]/g, " ")
.replace(/'(?=[^%]*%>)/g,"\t")
.split("'").join("\\'")
.split("\t").join("'")
.replace(/<%=(.+?)%>/g, "'; out += $1; out += '")
.split("<%").join("';")
var _ = require('underscore');
function sortByWeight (arr, weightMap) {
arr.sort(function (e1, e2) {
var w1 = weightMap[e1];
var w2 = weightMap[e2];
if (_.isNumber(w1) && _.isNumber(w2)) {
return w1 - w2;
} else {
return 0;
@xuyang2
xuyang2 / gist:9932749
Created April 2, 2014 12:02
underscore mixins
_.mixin({
/**
* Iterates over an array of numbers and returns the sum. Example:
*
* var sum = _.sum([1, 2, 3]);
* => 6
*
* @param obj
* @returns {Number}
*/
#!/bin/sh
# one way (older scala version will be installed)
# sudo apt-get install scala
#2nd way
sudo apt-get remove scala-library scala
wget www.scala-lang.org/files/archive/scala-2.10.3.deb
sudo dpkg -i scala-2.10.3.deb
sudo apt-get update
import junit.framework.Assert;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.crypto.engines.RC4Engine;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.Before;
import org.junit.Test;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* @see http://www.java2s.com/Code/Java/Security/JCEalgorithmsinBrowser.htm
*/
import java.awt.Dimension;
import java.security.Provider;
import java.security.Security;
import java.util.HashSet;
import java.util.Set;
@xuyang2
xuyang2 / qs.js
Created July 25, 2014 11:32
Angular query string utilities
// expose angular query string utilities
(function () {
'use strict';
var module = angular.module('myQueryString', []);
module.factory('myQueryString.QueryString', [function () {
var forEach = angular.forEach;
var hasOwnProperty = angular.hasOwnProperty;
var isArray = angular.isArray;
@xuyang2
xuyang2 / benchmark.py
Created October 15, 2015 08:01 — forked from eamartin/benchmark.py
Python JSON Benchmark
'''cjson, jsonlib, simplejson, and yajl also use C code
demjson did not use C code, but was too painfully slow to benchmark
(took about 20 seconds for these tests)
'''
import json
import sys
import time
with open('doc.json') as f:
/**
*
* Base64 encode / decode
* http://www.webtoolkit.info/
*
**/
var Base64 = {
// private property