Skip to content

Instantly share code, notes, and snippets.

View blasten's full-sized avatar

Emmanuel Garcia blasten

  • c.ai
  • Palo Alto, CA
View GitHub Profile
@blasten
blasten / extend.js
Last active August 29, 2015 14:03
Extends a function object used as a class Constructor
var extend = function(obj) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0, len = args.length; i < len; i++) {
for (var prop in args[i]) {
obj[prop] = args[i][prop];
}
}
};
@blasten
blasten / comments.sublime-snippet
Last active August 29, 2015 14:03
Comment snippet for Sublime Text 3
<snippet>
<content>
<![CDATA[
/**
* ${1:Method Description}
*
* @param {${2:argumentType}} ${3:argumentName} ${3:argumentDesc}
* @return {${4:returnType}}
*/
]]>
@blasten
blasten / module.js
Last active August 29, 2015 14:03
Factory pattern for node and the browser
(function(root, factory) {
if (typeof exports == 'object') {
// I'm Node
module.exports = factory(require('./lib'));
} else {
// I'm the Browser
factory(root.lib);
}
}(this, function(lib) {
@blasten
blasten / git.sh
Last active August 29, 2015 14:03
GIT useful commands
# Download the lastest from remote without trying to merge
# or rebase and reset the master branch to what was fetched
git fetch --all && git reset --hard origin/master
# Show files in the index that are unmerged
git ls-files -u
# Clone a branch
git clone -b Branch-name Repo-Url
@blasten
blasten / cs.js
Last active August 29, 2015 14:06
Computer Science fundamentals in JavaScript
// Build a tree using a compacted object with no pointer for the left/right children
function buildTree(A) {
// Shuffle the array. so we gain a more balanced search tree
A.sort(function() { return parseInt(Math.random()*3) - 1; });
var tree = {'0': A[0]}, current = 1;
var inserted, node, treeSize = 1;
// This will take O(N * log N) if we have a balanced tree
// the worst case cost will be O(N^2)
@blasten
blasten / string2decimal.js
Last active August 29, 2015 14:08
Converts words to a decimal value. e.g. one thousand five hundred fifty two and five cents = 1552.05
function getValue(str) {
switch (str) {
case 'one':
return 1;
case 'two':
return 2;
case 'three':
return 3;
case 'four':
return 4;
@blasten
blasten / prefix.js
Last active August 29, 2015 14:10
Gets the longest prefix from a string in linear time as in the KMP algorithm
function longestPrefix(str) {
// create a table of size equal to the length of `str`
// table[i] will store the prefix of the longest prefix of the substring str[0..i]
var table = new Array(str.length);
var maxPrefix = 0;
// the longest prefix of the substring str[0] has length
table[0] = 0;
// for the substrings the following substrings, we have two cases
@blasten
blasten / KMP.js
Last active February 11, 2024 04:04
String matching based on the KMP algorithm. This how `String.prototype.indexOf` is generally implemented.
// Construct a table with table[i] as the length of the longest prefix of the substring 0..i
function longestPrefix(str) {
// create a table of size equal to the length of `str`
// table[i] will store the prefix of the longest prefix of the substring str[0..i]
var table = new Array(str.length);
var maxPrefix = 0;
// the longest prefix of the substring str[0] has length
table[0] = 0;
@blasten
blasten / indexed-trees.js
Last active August 29, 2015 14:11
Indexed Trees implementation in JavaScript
// For more info about indexed trees visit
// http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees
//
// Sample run:
// var tree = new IndexedTree([2, 5, 6, 9])
// tree.getSumIn(2, 3) -> 15
//
// Why this is cool? It gives the sum of the values of a subarray in O(log n) time,
// the update of a value also runs in O(log n)
@blasten
blasten / intervals.js
Last active August 29, 2015 14:12
Detect elements within a bounding box.
// How fast can you get all the intervals that overlap with another interval?
// WTF.. who cares. Well if you are given the task to find all the stories
// from a facebook timeline visible to the user,
// how would you do that without blocking the user's scrolling?
// Imagine, you mark the stories as read and notify the server that the user saw those stories.
// It turns out you can achieve this task in log time using a segment tree.
// More about segment trees:
// http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor#Segment_Trees
// Sample: