Skip to content

Instantly share code, notes, and snippets.

View youtalk's full-sized avatar

Yutaka Kondo youtalk

View GitHub Profile
@youtalk
youtalk / SharedTaskExecutor.java
Created January 12, 2012 06:15
Thread pool using java.util.concurrent packages
package jp.youtalk;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class SharedTaskExecutor {
private static ExecutorService EXECUTORS =
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
@youtalk
youtalk / node-ja-sanitize.js
Created January 12, 2012 06:35
Sanitizing and Japanese full/half-width character converting for Node.js
var validator = require('validator');
var hankaku = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';
var zenkaku = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';
var zenkakuToHankaku = function (word) {
for (var i = 0, n = zenkaku.length; i < n; i++) {
word = word.replace(new RegExp(zenkaku[i], 'gm'), hankaku[i]);
}
return word.replace(/^\s+|\s+$/g, ''); // trim head and tail white space
};
@youtalk
youtalk / node-error-handling.js
Created January 12, 2012 06:37
Error handling for Node.js
var NotFoundError = module.exports.NotFoundError = function (message) {
this.name = 'NotFoundError: ' + message;
Error.call(this, message);
Error.captureStackTrace(this, arguments.callee);
};
NotFoundError.prototype.__proto__ = Error.prototype;
var UnauthorizedError = module.exports.UnauthorizedError = function (message) {
this.name = 'UnauthorizedError: ' + message;
Error.call(this, message);
Error.captureStackTrace(this, arguments.callee);
@youtalk
youtalk / node-jquery-autocomplete.js
Created January 12, 2012 06:40
Keyword search using Node.js server and jQuery-autocomplete client
// Server-side
app.get('/keyword/search',
function (req, res, next) {
var query = new RegExp('^' + req.query.q + '.*$', 'i');
var result = [];
mongodb.keywords.find(
{ word: query }, [ 'word' ], { limit: 5 },
function (err, keywords) {
keywords.forEach(function (k) {
result.push(k.word);
@youtalk
youtalk / SocketAcceptor.java
Created January 12, 2012 06:43
Server-client programs for socket communication in Java
package jp.youtalk;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
public class SocketAcceptor implements Callable {
public static final int INTERVAL_MS = 1000;
public static final int MAX_TRIAL = 100;
private final String host;
@youtalk
youtalk / Makefile
Created February 23, 2012 13:08
Makefiles for compiling c++
TARGET = name
CXXFLAGS = -O2 -g -Wall
LIBS = -lboost_thread -lboost_filesystem ¥
-I/usr/include/octave-`octave-config -v` ¥
-L/usr/lib/octave-`octave-config -v` -loctave -lcruft ¥
-I/usr/local/include -L/usr/local/lib -lode -ldrawstuff -lglut -l3ds
SRCS = $(shell ls *.cpp)
@youtalk
youtalk / NullRunnable.java
Created February 23, 2012 14:08
Singleton pattern for implementing null Runnable
package jp.youtalk;
public enum NullRunnable implements Runnable {
INSTANCE;
@Override
public void run() {}
}
@youtalk
youtalk / Makefile
Created February 24, 2012 05:21
Makefiles for compiling tex
TARGET = name
SOURECE = $(shell ls eps/*.eps)
LATEX = platex
DVIPDFM = dvipdfmx
XDVI = xdvi
.SUFFIXES: .tex .dvi .pdf
$(TARGET).dvi: $(TARGET).aux $(SOURECE)
$(LATEX) $(TARGET)
@youtalk
youtalk / YahooWebSearchAPI.java
Created February 24, 2012 12:20
Web search using Yahoo Web Search API
package jp.youtalk;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
@youtalk
youtalk / ping.js
Created May 10, 2012 12:20
Ping by Node.js to restart your Heroku hosting site
var request = require('request');
(function ping () {
request.get({ url: 'http://YourWebApp.herokuapp.com' }, function (err, res, body) {
if (!err && res.statusCode === 200) {
console.log('done');
}
});
})();