Skip to content

Instantly share code, notes, and snippets.

View alextanhongpin's full-sized avatar

Alex Tan Hong Pin alextanhongpin

View GitHub Profile
@alextanhongpin
alextanhongpin / serial-promise.js
Created November 11, 2017 18:25
Serial promise with bluebird. The next function will only execute after the first is completed.
const Promise = require('bluebird')
function doWork (val) {
return new Promise(resolve => {
setTimeout(() => {
resolve(val)
}, 2000)
})
}
@alextanhongpin
alextanhongpin / dynamic-mysql.js
Last active November 9, 2017 08:06
Simple code to concatenate multiple conditions for mysql statement
const conditions = [
['', '', ''],
['A', '', ''],
['', 'B', ''],
['', '', 'C'],
['A', 'B', ''],
['', 'B', 'C'],
['A', '', 'C'],
['A', 'B', 'C'],
].forEach(([param1, param2, param3]) => {
@alextanhongpin
alextanhongpin / boyer-moore.js
Created November 6, 2017 15:19
Boyer–Moore major­ity vote algorithm. In its sim­plest form, the algo­rithm finds a major­ity ele­ment, if there is one: that is, an ele­ment that occurs repeat­edly for more than half of the ele­ments of the input. How­ever, if there is no major­ity, the algo­rithm will not detect that fact, and will still out­put one of the ele­ments. A ver­si…
// Majority vote algorithm
// Objective: Given an array of integer, write an algorithm to find the majority element in it (if exist)
// Majority element: If an element appears more than n / 2 times in array where n is the size of the array
function BoyerMoore () {
let i = 0
let m
return {
vote (x) {
if (i === 0) {
@alextanhongpin
alextanhongpin / autocomplete.js
Created October 31, 2017 16:20
Experimenting autocomplete in js using Jaccard Similarity
const collection = ['food', 'car', 'paper', 'javascript', 'blockchain']
function search (keyword) {
return collection.filter((val) => val.toLowerCase().includes(keyword.toLowerCase()))
}
function autocorrectSearch (keyword) {
if (!search(keyword).length) {
const output = collection.filter((val) => {
const keywords = keyword.split('')

Keybase proof

I hereby claim:

  • I am alextanhongpin on github.
  • I am alextanhongpin (https://keybase.io/alextanhongpin) on keybase.
  • I have a public key ASBBjA5fR2Pv8scOYwIw0gSPnE43c7o8FW1UlCCCmfClvwo

To claim this, I am signing this object:

@alextanhongpin
alextanhongpin / index.js
Created October 3, 2017 03:45
Simple mysql script for inserting fake data
const faker = require('faker')
const mysql = require('mysql2/promise')
// function createRow () {
// return {
// column: faker.database.column(),
// type: faker.database.type(),
// collation: faker.database.collation(),
// engine: faker.database.engine()
@alextanhongpin
alextanhongpin / dump.go
Created September 28, 2017 23:30
Simple script to dump http request
import "net/http/httputil"
func main() {
dump := func(r *http.Request) {
output, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Println("Error dumping request:", err)
return
}
fmt.Println(string(output))
@alextanhongpin
alextanhongpin / nginx.conf
Created September 20, 2017 08:24
The default nginx config for AWS Elasticbeanstalk
# Elastic Beanstalk Managed
# Elastic Beanstalk managed configuration file
# Some configuration of nginx can be by placing files in /etc/nginx/conf.d
# using Configuration Files.
# http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/customize-containers.html
#
# Modifications of nginx.conf can be performed using container_commands to modify the staged version
# located in /tmp/deployment/config/etc#nginx#nginx.conf
@alextanhongpin
alextanhongpin / precise-time.js
Last active September 13, 2017 07:54
precise time
// return precise time in milliseconds, good for benchmarking
const PreciseTime = () => {
const hrTime = process.hrtime()
// time is an array [seconds, nanoseconds]
return hrTime[0] * 1000 + hrTime[1] / 1000000000
}
module.exports = PreciseTime
@alextanhongpin
alextanhongpin / append.go
Last active September 8, 2017 06:58
Useful go tips
package main
import (
"fmt"
)
func main() {
var a []string= []string{"a", "b"}
var b []string = []string{"c", "d"}
fmt.Println("Hello, playground", append(a, b...))