Skip to content

Instantly share code, notes, and snippets.

View Williammer's full-sized avatar

William He Williammer

View GitHub Profile
@Williammer
Williammer / silicon-valley-tricky.c
Last active April 29, 2016 16:46
silicon-valley-tricky.c
#include <stdio.h>
#include <stdlib.h>
typedef unsigned long u64;
/* Start here */
typedef void enc_cfg_t;
typedef int enc_cfg2_t;
typedef __int128_t dcf_t;
@Williammer
Williammer / reduce-reorganize-entry.js
Last active May 9, 2016 15:47
Functional programming - reduce [iterate, mutate inside and outside] - sample cases
var fs = require('fs')
var out = fs.readFileSync('src.txt', 'utf8')
.trim().split('\n')
.reduce(function (acc, cur) {
var curArr = cur.split(' ');
if (typeof acc[curArr[0]] !== 'object') {
acc[curArr[0]] = [];
}
@Williammer
Williammer / algorithm.DS.Graphs.DeepFirstSearch.js
Last active June 5, 2016 04:57
Graphic related algorithm
function cat() {
var head = _.first(arguments);
if (existy(head))
return head.concat.apply(head, _.rest(arguments));
else
return [];
}
function construct(head, tail) {
return cat([head], _.toArray(tail));
@Williammer
Williammer / algorithm.binarySearch.js
Created April 7, 2016 00:21
Algorithm Search Implementations
function binSearch(arr, data) {
var upperBound = arr.length - 1;
var lowerBound = 0;
while (lowerBound <= upperBound) {
var mid = Math.floor((upperBound + lowerBound) / 2);
if (arr[mid] < data) {
lowerBound = mid + 1;
} else if (arr[mid] > data) {
upperBound = mid - 1;
} else {
@Williammer
Williammer / jsPatterns.thunkify.js
Last active November 21, 2016 03:32
Thunk pattern - which seems to be one example of Partial application, is subroutine that helps.
function thunkify(fn) {
return function() {
var args = [].slice.call( arguments ),
ctx = this;
return function(cb) {
var called;
args.push(function(){
if (called) return;
@Williammer
Williammer / es6.generator.delegate.js
Last active March 25, 2016 08:48
Use case - ES6.generator -- Internally Iterator with the capacity of interception.
function* g4() {
yield* [1, 2, 3];
return "foo";
}
var result;
function* g5() {
result = yield* g4();
}
@Williammer
Williammer / Algorithm.DS.binarySearchTree.Impl.cpp
Last active June 25, 2016 08:45
Binary Search Tree Impl and Binary Search related algorithm. PreOrder: Traverse from Root to left sub-tree, then right sub-tree. InOrder: from smallest to largest item,which means it started from left-most leaf. PostOrder: leafs->left subRoot-> rightLeafs-> right subRoot --> Root ??Order: Traverse from Root to each sub-root(left then right).
struct Node{
int data;
Node *left, *right;
};
void preOrderPrint(Node *root)
{
print(root->name); //record root
if (root->left != NULL) preOrderPrint(root->left); //traverse left if exists
if (root->right != NULL) preOrderPrint(root->right);//traverse right if exists
}
@Williammer
Williammer / algorithm.DS.queue-arr.js
Last active July 8, 2018 09:20
Normal data structures implemented in languages like javascript etc. plz note that object based data structure perform faster than the array based data structure.
var queue = [];
queue.push(2); // [2]
queue.push(5); // [2, 5]
var i = queue.shift(); // [5] // i = 2
@Williammer
Williammer / jsDesignPattern.observer.js
Created April 7, 2015 15:00
example of observer pattern.
"use strict";
var publisher = {
subscribers: {
any: [] // event type: subscribers
},
subscribe: function (fn, type) {
type = type || 'any';
if (typeof this.subscribers[type] === "undefined") {
this.subscribers[type] = [];
}