Skip to content

Instantly share code, notes, and snippets.

/*
依序讀入data, 並以linked list 串接, 印出來
*/
function Node(data) {
this.data = data;
this.next = null;
}
@tingwei628
tingwei628 / Trie.js
Created August 7, 2016 04:24
Demo how to write Trie
function TrieNode() {
this.key = null;
this.children = {};
this.isNode = false;
}
function insert(word, r) {
var start = r;
for(var index = 0, le = word.length; index<le; index++) {
if (!start.children.hasOwnProperty(word[index])) {
var t = new TrieNode();
@tingwei628
tingwei628 / route.js
Last active September 25, 2016 05:48
Handle routes without React-Router
// babel-core 6.x
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import createHistory from 'history/createBrowserHistory';
import pathToRegexp from 'path-to-regexp';
/*
your route setting here
*/
@tingwei628
tingwei628 / progressbar.js
Last active October 5, 2016 15:05
website progress bar
(function() {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();
var start = null;
var d = document.getElementById("SomeElementYouWantToAnimate");
d.style.backgroundColor = 'red';
@tingwei628
tingwei628 / crawl.js
Created October 7, 2016 09:29
Node.js scan all Chinese words
var fs = require('fs');
var path = require('path');
var targetPath = 'D:\\p'; // 檔案路徑
(function crawl() {
var results = [];
fs.readdir(targetPath, function (err, list) {
if (err) {
throw err;
}
console.log("\n[" + list + "]");
@tingwei628
tingwei628 / example.js
Created October 11, 2016 08:56
Node.js use html(which includes javascript)
function a() {
alert('a');
}
a();
function sequentialCallback (...fns) {
return (...args) => {
const done = args.pop()
const next = (error, ...args) => {
if (error) return done(error)
if (fns.length) {
const fn = fns.shift()
return fn(...args, next)
}
return done(null, ...args)
/*
問題: 一個陣列裡面的物件要怎麼過濾出相同的key value?
例如: var array = [{a:1, b:1}, {a:1, b:2}, {a:2, b:1}, {a:1, b:1}];
(同時考慮 a, b的值)
*/
// by @Hao Yu Liao
public class MultiKeyDictionary<K1, K2, V>
{
private Dictionary<K1, Dictionary<K2, V>> baseDictionary = new Dictionary<K1, Dictionary<K2, V>>();
public IEnumerable<V> Values { get { return baseDictionary.Values.SelectMany(e => e.Values); } }
/*
This code is from
https://remysharp.com/2010/07/21/throttling-function-calls/#comment-497362
*/
function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;