Skip to content

Instantly share code, notes, and snippets.

Avatar

Chris Van Horne cwvh

  • Seattle, WA, USA
View GitHub Profile
@cwvh
cwvh / A.hs
Last active Dec 27, 2015
SSEM simulator in Haskell
View A.hs
{-# LANGUAGE BangPatterns #-}
import Data.Array.IO
import Data.Array.Base
import Data.Bits
import Data.Int (Int32)
main = do
!inits <- return . map (fromIntegral . bin2signed) . lines =<< getContents
program <- newListArray (0,31) inits :: IO (IOUArray Int Int)
v <- ssem program
View gist:7281634
00000000000000000000000000000000
01000000000000000000000000011000
01100000000000000000000000011010
01000000000000000000000000011010
01100000000000000000000000011011
01000000000000000000000000010111
10000000000000000000000000011011
11000000000000000000000000000000
00100000000000000000000000010100
10000000000000000000000000011010
View gist:7281488
stack = []
for line in sys.stdin:
dec = int('0b' + line, 2)
signed = struct.unpack('=l', struct.pack('=L', dec & 0xffffffff))[0]
stack.append(signed)
@cwvh
cwvh / .ghci
Last active Dec 23, 2015
Read header comment on how to setup.
View .ghci
{-
Place contents of this file in $HOME/.ghci then run
$ cabal install -j2 hoogle
$ hoogle data
-}
let ghciEscapeShellArg arg = "'" ++ concatMap (\c -> if c == '\'' then "'\"'\"'" else [c]) arg ++ "'"
:def! h return . (":! hoogle --color --count=20 " ++) . ghciEscapeShellArg
View x.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ccan/list/list.h>
struct context {
struct list_head connections;
int num_connections;
};
@cwvh
cwvh / x.cpp
Created May 26, 2013
parallel features of C++11
View x.cpp
#include <iostream>
#include <future>
#include <vector>
#include <algorithm>
#include <numeric>
#include <chrono>
template<typename Ran>
int parallel_sum(Ran b, Ran e)
{
View concurrent.hpp
#include <exception>
#include <future>
#include <thread>
#include <tbb/concurrent_queue.h>
namespace {
template<typename Future, typename F, typename T>
void set_value(std::promise<Future>& p, F& f, T& t) {
p.set_value(f(t));
}
View gist:1719998
class Comonad w where
(=>>) :: w a -> (w a -> b) -> w b
extract :: w a -> a
data T a = L a | B (T a) a (T a) deriving (Eq, Show)
unit :: a -> T a
unit = L
counit :: T a -> a
View gist:1651276
#!/usr/bin/env bash
# $ gostart x
# vim x.go
FILENAME="$1"
cp "$GOROOT"/doc/Makefile .
sed -i '' -e "s/tmpltohtml/"$FILENAME"/g" -e 's/\.\./$\(GOROOT\)/' -e '/^#.*/d' Makefile
touch "$1".go
View gist:1495096
@pattern(r'^([a-zA-Z_\$\%][a-zA-Z_0-9\{\}]*)\s*:(?<=:)(?::([a-zA-Z_\$][a-zA-Z_0-9\{\}]*)?:)?\s*(.*)$')
def target(match, env=env):
labels = ('name', 'dep', 'components')
groups = dict(zip(labels, match.groups()))
for k, v in groups.iteritems():
groups[k] = env.interp(v) if v else v
name, dep = groups['name'], groups['dep']
components = re.findall('\w+', groups['components'])
makefile.addtarget(name, dep, components)