Skip to content

Instantly share code, notes, and snippets.

@tomerfiliba
tomerfiliba / construct++.cpp
Created February 25, 2014 19:54
Consturct++: Pickler combinators in C++11
#include <iostream>
#include <sstream>
#include <cstdint>
#include <cstddef>
#include <string>
#include <memory>
#include <vector>
#include <tuple>
#include <utility>
#include <type_traits>
VlanTagOrType = Struct("VlanTagOrType",
Enum(UBInt16("type"),
IPv4 = 0x0800,
ARP = 0x0806,
RARP = 0x8035,
X25 = 0x0805,
IPX = 0x8137,
IPv6 = 0x86DD,
VLAN_TAG = 0x8100,
_default_ = Pass,
@tomerfiliba
tomerfiliba / gist:3773288
Created September 23, 2012 22:33
mumble
import threading
MAPPINGS = {
"&" : "&amp;",
"'" : "&apos;",
'"' : "&quot;",
"<" : "&lt;",
">" : "&gt;",
}
@tomerfiliba
tomerfiliba / gist:3698403
Created September 11, 2012 13:20
get the indexes of the top n elements in a numpy 2d-array
import numpy as np
import bottleneck as bn
def top_n_indexes(arr, n):
idx = bn.argpartsort(arr, arr.size-n, axis=None)[-n:]
width = arr.shape[1]
return [divmod(i, width) for i in idx]
np.random.seed(47)
@tomerfiliba
tomerfiliba / gist:2693796
Created May 14, 2012 12:56
The most efficient way to compute powers of two!
>>> x=""
>>> for i in range(25):
... print i, len(x)
... x = repr(x)
...
0 0
1 2
2 4
3 8
4 16
@tomerfiliba
tomerfiliba / tailcal.py
Created January 25, 2012 15:03
Tail call optimization
#
# By Nimrod Aviram and Eyal Lotem
#
import functools
class TailCallFunc(object):
def __init__(self, func):
self.func = func
{-# LANGUAGE ScopedTypeVariables #-}
--module Construct where
import Prelude hiding (const, repeat)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Word (Word8, Word16, Word32, Word64)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Binary as BIN
import Data.Bits (Bits, bitSize, shiftR)
from utils import singleton
from monads import monadic, MonadRunner
class Text(object):
def __init__(self, text):
self.text = text
class Input(object):
def __init__(self, label):
self.label = label
@tomerfiliba
tomerfiliba / gist:1239758
Created September 24, 2011 19:41
Foxx0rz
o
(s
);
P(c);
N();;
exe(p); //
typedef //u
/*jj>*/ /*op
*/ unsigned char
nf ; main(){ char ch[
@tomerfiliba
tomerfiliba / gist:1152878
Created August 17, 2011 23:09
My first Haskell program
{- learning haskell by "porting" Construct from python -}
import Data.Int
import Data.Word
import qualified Data.ByteString.Lazy as LBS
import Data.Char
import Control.Monad
import Data.Binary.Get
data Packer t = Packer {