Skip to content

Instantly share code, notes, and snippets.

@nicky-zs
nicky-zs / church-nums.ss
Created January 7, 2014 08:57
Church Numerals implementation in scheme.
;; A Church-Numberal is a function, which takes a function f(x)
;; as its argument and returns a new function f'(x).
;; Church-Numerals N applies the function f(x) N times on x.
; predefined Church-Numerals 0 to 9
(define zero (lambda (f) (lambda (x) x)))
(define one (lambda (f) (lambda (x) (f x))))
(define two (lambda (f) (lambda (x) (f (f x)))))
(define three (lambda (f) (lambda (x) (f (f (f x))))))
(define four (lambda (f) (lambda (x) (f (f (f (f x)))))))
@nicky-zs
nicky-zs / readline.js
Created December 19, 2013 02:47
The readline functionality is not very well supported in nodejs for it's api level is unstable as well as it is treated for tty use first. Here is a simple segment to readline from stdin. If you want to readline from a file, either changing the code with fs.createReadStream or just redirecting input from bash can do.
var readline = require('readline');
function main() {
readline.createInterface({
input: process.stdin,
terminal: false,
}).on('line', function(line) {
console.log(line);
});
}
@nicky-zs
nicky-zs / memcpy.c
Created November 19, 2013 06:31
One way to solve the glibc compatibility problem. In my case, when building a program with libthrift.a on linux with glibc version 2.15, ld always links memcpy@GLIBC_2.14 which cannot be found on systems with glibc version < 2.14. So, use this file to define a symbol __wrap_memcpy and use -Wl,--wrap=memcpy to tell ld using this symbol when meeti…
#include <string.h>
void *__memcpy_glibc_2_2_5(void *, const void *, size_t);
asm(".symver __memcpy_glibc_2_2_5, memcpy@GLIBC_2.2.5");
void *__wrap_memcpy(void *dest, const void *src, size_t n)
{
return __memcpy_glibc_2_2_5(dest, src, n);
}
@nicky-zs
nicky-zs / multipart.py
Created September 23, 2013 05:43
Build multipart/form-data with files.
#vim: fileencoding=utf-8
import mimetools
import itertools
class MultiPartForm(object):
def __init__(self):
self.form_fields = []
self.files = []
@nicky-zs
nicky-zs / make_safely_shutdown.py
Created August 22, 2013 09:10
Make the tornado httpserver.HTTPServer to be shutdown safely.
# vim: fileencoding=utf-8
import time, signal
from tornado import web, ioloop, options, httpserver
_SHUTDOWN_TIMEOUT = 30
def make_safely_shutdown(server):
io_loop = server.io_loop or ioloop.IOLoop.instance()
def stop_handler(*args, **keywords):
def shutdown():
@nicky-zs
nicky-zs / run_background.py
Last active December 21, 2015 04:08
Run costly tasks in the background in tornado, along with tornado.gen.engine.
# vim: fileencoding=utf-8
import sys, time
from multiprocessing.pool import ThreadPool
from tornado import web, gen, stack_context, httpserver, ioloop, util, options
_workers = ThreadPool()
def costly_task(n): # a costly task like processing images
time.sleep(n)
return n