Skip to content

Instantly share code, notes, and snippets.

View zsol's full-sized avatar

Zsolt Dollenstein zsol

View GitHub Profile
@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 ()
@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 / 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 / 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 / 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;