Skip to content

Instantly share code, notes, and snippets.

@zsol
zsol / nth_iterator.hpp
Created July 6, 2011 07:44
nth_iterator - an iterator wrapper that only uses every nth element returned by the underlying iterator
template <int n, typename Iterator>
class nth_iterator
{
public:
typedef typename std::iterator_traits<Iterator>::iterator_category iterator_category;
typedef typename std::iterator_traits<Iterator>::value_type value_type;
typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
typedef typename std::iterator_traits<Iterator>::pointer pointer;
typedef typename std::iterator_traits<Iterator>::reference reference;
typedef Iterator iterator_type;
@zsol
zsol / gist:1112252
Created July 28, 2011 18:53
Solution for problem 24
int ints[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int i = 1; i < 1000000; ++i) std::next_permutation(ints, ints+10);
for (int i = 0; i < 10; ++i) std::cout << ints[i];
@zsol
zsol / SubListOf.hs
Created June 2, 2012 21:22
subListOf
prefixOf [] _ = True
prefixOf (x:xs) (y:ys) = x == y && xs `prefixOf` ys
prefixOf _ _ = False
subListOf l [] = null l
subListOf l m@(_:ys) = l `prefixOf` m || l `subListOf` ys
--not sure if ghc transforms the above, but here's a tail recursive version
subListOf' l [] = null l
subListOf' l m@(_:ys)
@zsol
zsol / Parser.hs
Created June 3, 2012 08:48
parse logs
{-# LANGUAGE NoMonomorphismRestriction #-}
import Text.Parsec
import Data.List.Split (splitOn)
--field = lexeme $ noneOf " "
field = manyTill anyChar space
logline = do
try (do
@zsol
zsol / gist:3085658
Created July 10, 2012 19:24
Parsers
{-# LANGUAGE NoMonomorphismRestriction, FlexibleContexts #-}
module Parsers
(getCategoryAndEvent, getFields)
where
import Data.Attoparsec.Lazy
import qualified Data.ByteString.Lazy.Char8 as B
import qualified Data.ByteString.Char8 as SB
import Data.List (sort)
spaces :: Parser ()
{badmatch,
{error,
{{{function_clause,
[{rabbit_queue_index,journal_minus_segment1,
[{no_pub,del,ack},undefined]},
{rabbit_queue_index,'-journal_minus_segment/2-fun-0-',4},
{array,sparse_foldl_3,7},
{array,sparse_foldl_2,9},
{rabbit_queue_index,'-recover_journal/1-fun-0-',1},
{dict,map_bucket,2},
@zsol
zsol / gist:3397650
Created August 19, 2012 20:51
PSQueue stack overflow
import Data.PSQueue
data Element = E Int String deriving (Show)
type Buffer = PSQ Int Element
instance Eq Element where
(E a _) == (E b _) = a == b
instance Ord Element where
(E a _) < (E b _) = a < b
@zsol
zsol / gist:3682934
Created September 9, 2012 06:20
script to check for kafka consumer lag
from kazoo.client import KazooClient
config = {
'consumer_group': '<%= @consumer_group %>',
'topic': '<%= @topic %>'
}
def main():
zk = KazooClient()
zk.start()
@zsol
zsol / producer
Created October 9, 2012 20:32
carbon with 1s resolution
while true; do
echo "hlogster.test.count $i $(date +%s)" | /opt/graphite/bin/carbon-client.py localhost:2003:a;
echo $i
i=$((i+1))
i=$((i%10))
sleep 1
done
@zsol
zsol / gist:4265895
Created December 12, 2012 07:49
unbounded queue based communication between producer and several consumers in haskell
-- tst
import Control.Concurrent.Chan
import Control.Concurrent
import Control.Exception (finally)
import System.IO.Unsafe (unsafePerformIO)
type Result = Int
execute_pig::String-> IO Result