Skip to content

Instantly share code, notes, and snippets.

@wjx
wjx / get_authors.js
Created Mar 24, 2017
Promise version answer to the EloquentJavascript Exercise 20.1
View get_authors.js
var http = require("http");
var Promise = require("promise");
function getAuthor(type) {
return new Promise(function(resolve, reject) {
var request = http.request({
hostname: "eloquentjavascript.net",
path: "/author",
method: "GET",
headers: {Accept: type}
View create-your-own-map.md

#Create your own map with OpenStreetMap

This is some note of me playing with OpenStreetMap and creating an Android app that renders the map drawn.

##Draw a map

###Get GPS trace

If you want to draw your own map, OpenStreetMap

@wjx
wjx / BBST.java
Last active Jan 16, 2017
Array to BST
View BBST.java
//http://www.geeksforgeeks.org/sorted-array-to-balanced-bst/
//is the better solution(return created node, instead of a separated
//insert operation).
//Note here int mid = (int)Math.floor(n / 2); is used to get the middle
//node instead of int mid = (start + end) / 2;, resulting right middle
//node is selected when there is even number of nodes in the array.
import java.util.*;
import java.lang.*;
import java.io.*;
@wjx
wjx / Promise-all.js
Created Dec 18, 2016
Alternative solution for EloquentJavaScript exercise 17.2
View Promise-all.js
function all(promises) {
return new Promise(function(success, fail) {
// Your code here.
function promiseThen(i) {
return promises[i].then(function(res) {
//we should return the res here.
//push into results here won't work, since
//promises here settles at different time
//and elements in results will be out of order.
return res;
@wjx
wjx / content-negotiation.js
Created Dec 12, 2016
Promise version of EloquentJavaScript Excercise 17.1
View content-negotiation.js
//Ref:
//https://developers.google.com/web/fundamentals/getting-started/primers/promises
function requestAuthor(type) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open("GET", "http://eloquentjavascript.net/author", true);
req.setRequestHeader("accept", type);
req.onload = function() {
if(req.status == 200) {
@wjx
wjx / precomputed-mirroring.js
Last active Dec 5, 2016
Implementation for book EloquentJavaScript chapter16 exercise "Precomputed mirroring"
View precomputed-mirroring.js
var playerSprites = document.createElement("img");
var extraCanvas = document.createElement("canvas");
var extraCx = extraCanvas.getContext("2d");
playerSprites.src = "img/player.png";
playerSprites.addEventListener("load", function(event) {
var width = playerSprites.width;
var height = playerSprites.height;
extraCx.scale(-1, 1);
extraCx.translate(-width, 0);
@wjx
wjx / mouse-trail.html
Created Nov 30, 2016
Implementing mouse trail using requestAnimationFrame for EloquentJavaScript Exercise 14.2
View mouse-trail.html
<!doctype html>
<style>
.trail { /* className for the trail elements */
position: absolute;
height: 6px; width: 6px;
border-radius: 3px;
background: teal;
}
body {
@wjx
wjx / secril-client-sap.cpp
Last active Aug 29, 2015
secril-client-sap.cpp
View secril-client-sap.cpp
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@wjx
wjx / stdio.c
Last active Aug 29, 2015
Android adb TEMP_FAILURE_RETRY
View stdio.c
//bionic/libc/stdio/stdio.c
int
__sread(void *cookie, char *buf, int n)
{
FILE *fp = cookie;
int ret;
ret = TEMP_FAILURE_RETRY(read(fp->_file, buf, n));
/* if the read succeeded, update the current offset */
if (ret >= 0)
View colordlogcat.py
#!/usr/bin/python
'''
Copyright 2009, The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
You can’t perform that action at this time.