Skip to content

Instantly share code, notes, and snippets.

@suxue
suxue / demo.js
Created March 11, 2014 04:51
simple phantomjs usage
var page = require('webpage').create();
page.open
page.open('http://example.com', function(status) {
var x = page.evaluate(function() {
return "-" + window.document.title + '-';
});
console.log(x);
phantom.exit(0);
});
@suxue
suxue / Ann.coffee
Created March 11, 2014 04:54
simple ann example
assert = require("assert")
_ = require("underscore")
sigmoid = (v) ->
return 1 / (1 + Math.E ** (-v))
pass = () -> undefined
class Ann
constructor: (inputs, hiddens, outputs) ->
@suxue
suxue / counter.coffee
Created March 11, 2014 04:56
simple MT in rhino/nashorn
Mutex = java.util.concurrent.locks.ReentrantLock
Thread = java.lang.Thread
mutex = new Mutex
sum = 0
adder = (c) ->
-> while c > 0
c -= 1
mutex.lock()
sum += 1
@suxue
suxue / Makefile
Created March 11, 2014 05:01
#jjs :call Java from js
run: Mutex.class
jjs -cp . counter.js
Mutex.class: Mutex.java
javac Mutex.java
.PHONY: run
@suxue
suxue / server.c
Created March 11, 2014 05:04
multi-process tcp echo server
#ifdef _win32_
#include <winsock2.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <strings.h>
#include <unistd.h>
#include <stdio.h>
@suxue
suxue / Makefile
Created March 11, 2014 05:06
simple rpc over tcp, for vm integrating?
CC=clang -std=c99
CFLAGS=-D_GNU_SOURCE -Wall -g -O2
CXX=clang++ -std=c++03
CXXFLAGS=-Wall -g -O2 `pkg-config --cflags QtDBus`
LDFLAGS=`pkg-config --libs QtDBus`
all: server client
install: server
install -m755 server $(HOME)/.kde/Autostart/
@suxue
suxue / ipython.ipy
Created March 11, 2014 05:09
inter-operation between python and shell by using ipython script
#!/usr/bin/ipython
# vim: ft=python:
# to run it as ipython script, suffix must be .ipy
tmpfile = !mktemp
tmpfile = tmpfile[0]
files = !ls
file = open (tmpfile, "w")
file.writelines(files.n) # get it as newline separated string
file.close()
!cat $tmpfile # refer to python variable in shell
#!/bin/ksh
if [[ -z "$1" ]] || (( $1 <= 1024 )); then
echo 'usage: tcpserver port'
exit 1
fi
typeset port=$1
typeset pipe=`mktemp -u`
mkfifo $pipe
@suxue
suxue / WebSocketServer.sh
Created March 16, 2014 11:52
simple WebSocket Server make use of netcat
#!/bin/ksh
typeset port=$((RANDOM % 60000))
while ((port < 30000)); do
port=$((RANDOM % 60000))
done
typeset pipe=`mktemp -u`
mkfifo $pipe
trap "rm -f $pipe" INT
@suxue
suxue / Edit_Distance.py
Created March 19, 2014 09:09
Calculate Edit Distance
#!/usr/bin/env python
# vim: set fileencoding=utf-8:
import sys
import numpy
import random
class Direction:
def __str__(self):
if self._dir == 0: