Skip to content

Instantly share code, notes, and snippets.

@killwing
killwing / option_parser.sh
Created October 10, 2014 10:36
cmd line options parser
#!/bin/bash
# for optional arguments: short - there can't be any spaces between the option and the argument
# long - the argument can only be passed by "="
# parse options
OPTIONS=$(getopt -o o::r:n -l optional::,required:,none -n "$0" -- "$@")
# check result
[ $? -ne 0 ] && exit 1
@killwing
killwing / striker.html
Last active December 22, 2015 16:29
a HTML5 canvas based simplified copycat of the shooting game in Wechat 5.0
<!doctype html>
<html>
<head>
<title>striker</title>
</head>
<body align="center" style="margin:0;">
<canvas id="canvas" width="1500" height="900"></canvas>
<script>
// class inherit
function inherits(ctor, superCtor) {
@killwing
killwing / canvas-sketch.html
Last active December 21, 2015 09:28
character sketch in canvas
<!doctype html>
<head>
<title>Canvas Data</title>
</head>
<body>
<canvas id="canvas" width="1500" height="2000" style="position: absolute; top: 0px; left: 0px;"></canvas>
</body>
<script>
var theCanvas = document.getElementById('canvas');
var context = theCanvas.getContext('2d');
@killwing
killwing / getPrime.clj
Created March 24, 2013 13:45
[getPrime] kinds of primes generation
(defn lazy-primes []
(letfn [(enqueue [sieve n step]
(let [m (+ n step)]
(if (sieve m)
(recur sieve m step)
(assoc sieve m step))))
(next-sieve [sieve candidate]
(if-let [step (sieve candidate)]
(-> sieve
(dissoc candidate)
@killwing
killwing / slideshare-downloader.sh
Last active December 15, 2015 04:38 — forked from giudinvx/slideshare-downloader.sh
[slideshare-downloader] This script takes a slideshare presentation URL as an argument and carves all the slides in flash format, then they are converted to and finally merged as a PDF.
#!/bin/bash
# Author: Andrea Lazzarotto
# http://andrealazzarotto.com
# andrea.lazzarotto@gmail.com
# Slideshare Downloader
# This script takes a slideshare presentation URL as an argument and
# carves all the slides in flash format, then they are converted to
# and finally merged as a PDF
@killwing
killwing / Printtype.hpp
Last active December 15, 2015 00:09
[Printtype] String representation of a type from @marc Glisse
// extern "C" not handled
#include <typeinfo>
#include <type_traits>
#include <string>
template<class...> struct Printtype;
template<> struct Printtype<> {
std::string name() {
return "";
}
@killwing
killwing / ScopeGuard.hpp
Created February 6, 2013 09:44
[ScopeGuard] encapsulates scoped control flow from @andrei
#ifndef SCOPEGUARD_H
#define SCOPEGUARD_H
template <typename Fun>
class ScopeGuard {
public:
ScopeGuard(Fun f) : f_(std::move(f)), active_(true) {}
~ScopeGuard() {
if (active_) {
f_();
@killwing
killwing / Expected.hpp
Created February 6, 2013 08:29
[Expected] Make exceptions be the error codes! from @andrei
#ifndef EXPECTED_H
#define EXPECTED_H
#include <exception>
#include <typeinfo>
// Make exceptions be the error codes!
template <typename T>
class Expected {
public:
@killwing
killwing / escapeXml.cpp
Created November 20, 2012 07:48
[escapeXml] escape XML special chars
#include <iostream>
#include <string>
using namespace std;
std::string escapeXml(const std::string& s) {
std::string ret;
std::string::size_type i = 0;
std::string::size_type pos = 0;
for (; i != s.size(); ++i) {
std::string rep;
@killwing
killwing / Registry.js
Created November 19, 2012 08:00
[Registry] a simple registry without moving elements
#!/usr/local/bin/node
var Registry = function() {
this.r = [null];
};
Registry.prototype.register = function(object) {
var id = this.r[0];
if (id === null) {
this.r.push(object);