Skip to content

Instantly share code, notes, and snippets.

@wjx
wjx / get_authors.js
Created March 24, 2017 01:47
Promise version answer to the EloquentJavascript Exercise 20.1
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}

#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 January 16, 2017 01:54
Array to BST
//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 December 18, 2016 12:35
Alternative solution for EloquentJavaScript exercise 17.2
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 December 12, 2016 16:11
Promise version of EloquentJavaScript Excercise 17.1
//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 December 5, 2016 06:21
Implementation for book EloquentJavaScript chapter16 exercise "Precomputed mirroring"
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 November 30, 2016 14:03
Implementing mouse trail using requestAnimationFrame for EloquentJavaScript Exercise 14.2
<!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 August 29, 2015 14:21
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 August 29, 2015 14:20
Android adb TEMP_FAILURE_RETRY
//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)
#!/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