Skip to content

Instantly share code, notes, and snippets.

var x1=1, y1=1;
@smilingleo
smilingleo / gist:6518914
Last active December 22, 2015 19:19
CORS-enabled API
function createXHR(method, url) {
var xhr = new XMLHttpRequest();
if (xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
@smilingleo
smilingleo / equivalent_binary_tree.go
Last active September 8, 2015 00:32
Exercise: Equivalent Binary Trees (https://tour.golang.org/concurrency/8)
package main
import (
"golang.org/x/tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
@smilingleo
smilingleo / WordCounterTest.java
Created October 19, 2015 07:13
word counter implementation by different approaches.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.IntStream;
import java.util.stream.Stream;
@smilingleo
smilingleo / JavaTest.java
Created June 28, 2016 00:35
a way to get actual parameter type
@Test
public void testJavaGeneric() {
// have to use a empty `{ }`, so that the `getClass().getGenericSuperclass()` will return a `ParameterizedType` instance.
List<Integer> list = new LinkedList<Integer>() {
};
// superclass contains the unerased info.
Type supers = list.getClass().getGenericSuperclass();
assertTrue(supers instanceof ParameterizedType);
ParameterizedType paramType = (ParameterizedType) supers;
@smilingleo
smilingleo / GraphQLBatchFetcher.java
Last active June 12, 2017 02:27
GraphQL java client example, use batch data fetcher for performance optimization.
package leo.me.graphql;
/**
* Created by leo on 4/26/17.
*/
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLList.list;
import static graphql.schema.GraphQLObjectType.newObject;
@smilingleo
smilingleo / postgres-cheatsheet.md
Created June 5, 2017 15:25 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

If run with -E flag, it will describe the underlaying queries of the \ commands (cool for learning!).

Most \d commands support additional param of __schema__.name__ and accept wildcards like *.*

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* This is a test indicating the timezone issue for a datebase which is in a timezone which observes
* DST,
* for example, America/Los_Angeles.
@smilingleo
smilingleo / HttpProtocolComparison.java
Created March 27, 2018 15:36
Performance comparison between HTTP/1.1 and HTTP/2 using OkHttpClient
package leo.me;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
@smilingleo
smilingleo / props.js
Last active June 20, 2018 01:27
A drawIO plugin which parses the `tooltip` attribute in markdown format.
;/*! showdown v 2.0.0-alpha1 - 19-04-2018 */
var showdownFn = function(){
/**
* Created by Tivie on 13-07-2015.
*/
function getDefaultOpts (simple) {
'use strict';
var defaultOptions = {