Skip to content

Instantly share code, notes, and snippets.

@qzchenwl
qzchenwl / CEnum.hs
Last active November 6, 2015 10:54
Write C-style enum in Haskell
{-# LANGUAGE ViewPatterns #-}
module CEnum where
import Data.Maybe ( fromMaybe )
import Text.Read ( readMaybe )
import Text.Regex.PCRE
import Language.Haskell.TH
normalCons :: Con -> Name
@qzchenwl
qzchenwl / init-ubuntu-haskell.sh
Last active August 11, 2017 16:49
Install Haskell development environment.
#!/bin/bash
cat << EOF >> $HOME/.bashrc
export PATH="\$HOME/.local/bin:\$HOME/.cabal/bin:/opt/ghc/7.10.2/bin:/opt/cabal/head/bin:\$PATH"
alias tmux="tmux -2"
EOF
cat << EOF > $HOME/.gitconfig
@qzchenwl
qzchenwl / Pm.java
Created September 8, 2015 10:08
frameworks/base/cmds/pm/src/com/android/commands/pm
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@qzchenwl
qzchenwl / compressImage.html
Last active August 29, 2015 14:27
压缩图像示例
<input type="file" id="fileinput"/>
@qzchenwl
qzchenwl / l2tp.md
Last active October 22, 2016 13:29
在Ubuntu12.04上安装l2tp/ipsec VPN服务器

在Ubuntu12.04上安装l2tp/ipsec VPN服务器

因为众所周知的原因,l2tp也已沦陷,本文不再维护。

记录我在Ubuntu服务器上安装l2tp/ipsec VPN的过程,以供日后查询。ipsec用于验证和加密数据包,由openswan提供;l2tp即第二层隧道协议,由xl2tpd提供。

安装相关软件

默认配置即可,后面另有详细介绍。

module Main where
import Data.List
vertices = "ABCDEFGHIJK"
edges = ["ADB", "AEC", "AFHJ", "AGIK", "BJKC", "BHIE", "DFGE"]
combinations n = filter ((==n) . length) . subsequences
inSameEdge xs = or [ all (`elem` edge) xs | edge <- edges]
isTriangle (a:b:c:[]) = all inSameEdge [[a, b], [a, c], [b, c]] && not (inSameEdge [a, b, c])
triangles = filter isTriangle (combinations 3 vertices)
@qzchenwl
qzchenwl / filter-log.sh
Last active August 29, 2015 14:23
Filter HTTP request log by IP. Redirect log to file named with IP.
awk 'BEGIN{RS="\\+\\+\\+\\+\\+\\+\\+\\+\\+ Incoming Request \\+\\+\\+\\+\\+\\+\\+\\+\\+"; FS="\n"} { match($0, /X-Real-IP: ([0-9\.]+)/, ary); fn=ary[1]".txt"; print $0 >> fn }' debug.log
grep -l 'HTTP/1.1 412 Precondition Failed' *.txt | while read file; do mv $file 412; done
@qzchenwl
qzchenwl / main.cpp
Last active August 29, 2015 14:08
Single instance base class for C++.
#include <iostream>
#include "single_instance.hpp"
using namespace std;
class A : public enable_single_instance<A>
{
friend class enable_single_instance<A>;
public:
@qzchenwl
qzchenwl / 0_reuse_code.js
Last active August 29, 2015 14:06
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@qzchenwl
qzchenwl / Chan.hpp
Last active September 14, 2015 20:01
Haskell's MVar and Chan in C++
#include <memory>
#include "MVar.hpp"
template <typename T>
struct Item;
template <typename T>
struct Stream
{
typedef MVar<Item<T>> type;