Skip to content

Instantly share code, notes, and snippets.

@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;
#include <cassert>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
using namespace std;
int inverter_delay = 2;
@qzchenwl
qzchenwl / var_num.h
Last active December 31, 2015 02:19
variable num
#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#define VA_NUM_ARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, N, ...) N
#define MACRO_DISPATCHER(func, ...) MACRO_DISPATCHER_(func, VA_NUM_ARGS(__VA_ARGS__))
#define MACRO_DISPATCHER_(func, nargs) MACRO_DISPATCHER__(func, nargs)
#define MACRO_DISPATCHER__(func, nargs) func ## nargs
#define VAR1(x) L ## #x" = " << x
#define VAR2(x, ...) L ## #x" = " << x << L", " << VAR1(__VA_ARGS__)
#define VAR3(x, ...) L ## #x" = " << x << L", " << VAR2(__VA_ARGS__)
@qzchenwl
qzchenwl / Log.cpp
Last active December 27, 2015 16:19
Log stack trace for cpp
#include "Log.h"
#include <tchar.h>
namespace Log
{
__declspec(thread) Checkpoint* Checkpoint::s_pTop = NULL;
__declspec(thread) bool Checkpoint::exception_handled = false;
void PrintF_Internal(PCTSTR szFmt, const int* pParams, PTSTR szBuf, size_t nLenBuf, size_t nData)
@qzchenwl
qzchenwl / BlockingQueue.h
Last active December 27, 2015 08:49
event loop
#pragma once
#include <mutex>
#include <condition_variable>
#include <queue>
using namespace std;
template <typename T>
class BlockingQueue
@qzchenwl
qzchenwl / example.hs
Created October 27, 2013 11:30
这个题目是为了将10幢房子按放到一个10*10的格子中, 每个格子最多只能放一幢房子. 最终目的是使房子按放之后, 任何一幢房子的横轴,纵轴以及对角线都看不到其它的房子.
module Main where
-- 是否存在重复的元素
-- [1,2,3] -> False
-- [1,1,3] -> True
isUnique :: (Eq a) => [a] -> Bool
isUnique = isUnique' []
isUnique' :: (Eq a) => [a] -> [a] -> Bool
isUnique' _ [] = True
@qzchenwl
qzchenwl / Unit.hs
Last active December 25, 2015 09:19
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Unit where
import qualified Prelude as P
import Prelude hiding ((+), (*), (/), (-), Int, pi)
-- http://blog.tmorris.net/posts/20-intermediate-haskell-exercises/
module Main where
class Fluffy f where
furry :: (a -> b) -> f a -> f b
-- Exercise 1
-- Relative Difficulty: 1
instance Fluffy [] where
furry _ [] = []
module Main where
-----------------------------------------------------------------------------
---- |
----
---- 输出最长回文子串, 直观版。
----
-------------------------------------------------------------------------------
import Data.List (maximumBy)
import Data.Function (on)
@qzchenwl
qzchenwl / jq.hs
Created September 23, 2013 09:23
JSON Parser
module Main where
import Text.ParserCombinators.Parsec
import Control.Applicative ((*>), (<*), (<*>), (<$>), (<$), empty)
import Numeric (readHex, readSigned, readFloat)
run :: Show a => Parser a -> String -> IO ()
run p input = case (parse p "" input) of
Left err -> do putStr "parse error at "
print err