Skip to content

Instantly share code, notes, and snippets.

View ehamberg's full-sized avatar
🌴
On holiday until December

Erlend Hamberg ehamberg

🌴
On holiday until December
View GitHub Profile
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
unsigned int sharedPrefixLength(const char *a, const char *b);
std::string longestRepeatedString(const std::string s);
int main()
{
@ehamberg
ehamberg / prettyping.rb
Created January 11, 2014 14:08
Homebrew formula for prettyping.sh
require 'formula'
class Prettyping < ScriptFileFormula
homepage 'https://bitbucket.org/denilsonsa/small_scripts/overview'
url 'https://bitbucket.org/denilsonsa/small_scripts/raw/130bc1a6162fc14629cfd1cd30355d586cc38f5f/prettyping.sh'
sha1 '748ce4a5cfd098f728de1c4c374949a82302dcf1'
version '130bc1a'
def install
mv "prettyping.sh", "prettyping"
template <class T> struct Maybe { virtual ~Maybe() {}; };
template <class T> struct Nothing : public Maybe<T> {};
template <class T> struct Just : public Maybe<T> { Just(T v) : v(v) {} T v; };
template <class T>
T fromMaybe(const Maybe<T>& maybeVal, const T& defaultVal)
{
if (const Just<T>* j = dynamic_cast<const Just<T>*>(&maybeVal)) {
return j->v;
}
import Prelude hiding (lookup)
import Data.IORef
import Data.Maybe
type NatArray a = IORef (Int -> Maybe a)
newArray :: IO (NatArray a)
newArray = newIORef (const Nothing)
lookup :: NatArray a -> Int -> IO (Maybe a)
import Data.IORef
f ref = writeIORef ref 2
main = do
putStrLn "Look! Mutation!"
myRef <- newIORef 1
v <- readIORef myRef
(putStrLn . show) v
f myRef
mk_assert pred_fn (expected, actual, formatter) =
if pred_fn (expected, actual)
then "Test passed"
else "Expected '" ++ formatter expected ++ "' but got '" ++ formatter actual ++ "'"
assert_equals = mk_assert (uncurry (==))
pass2 = assert_equals(42, 42, show)
fail2 = assert_equals(1, 2, show)
data Set a = S { insert :: a -> Set a
, member :: a -> Bool
, size :: Int
}
emptySet = let insert xs n = makeSet (if n `elem` xs then xs else n : xs)
member xs n = n `elem` xs
size = length
makeSet xs = S (insert xs) (member xs) (size xs)
in makeSet []
@ehamberg
ehamberg / gist:3899289
Created October 16, 2012 13:30 — forked from adinapoli/gist:3899017
Iteratee attempt
{-# LANGUAGE OverloadedStrings #-}
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.Enumerator (Iteratee, Enumeratee, ($=), (=$), (=$=), (==<<))
import qualified Data.Enumerator as E
import qualified Data.Enumerator.Binary as EB
import qualified Data.Enumerator.Text as ET
import qualified Data.Enumerator.List as EL
import Data.Text
@ehamberg
ehamberg / 3wayqs.hs
Created September 6, 2012 12:41
Quicksort using 3-way partitioning
import Data.Vector (Vector (..), (!), (//))
import qualified Data.Vector as V
import Data.List (sort)
import Test.QuickCheck
-- partition the given array using 3-way partitioning
partition :: (Ord a) => Int -> Int -> Vector a -> (Vector a,Int,Int)
partition lo hi a = partition' lo lo hi a
where partition' i lt gt a
| i > gt = (a,lt,gt)
{-# Language OverloadedStrings #-}
import Prelude hiding (length)
import Data.Text
-- Calculates LCS of two strings without using a backtrack matrix. The helper
-- function lcs' does the actual work.
lcs :: Text -> Text -> Text
lcs s1 s2 = lcs' "" (length s1,length s2) s1 s2
where lcs' acc (x,y) s1 s2