This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename ForwardIterator> | |
void stable_sort_forward(ForwardIterator first, ForwardIterator last) | |
{ | |
using Range = std::pair<ForwardIterator, ForwardIterator>; | |
using Counter = binary_counter<merger<ForwardIterator>, Range>; | |
Counter c { merger<ForwardIterator>{}, { last, last } }; | |
accumulate_iter(first, last, &c, | |
[](auto c, auto it) { | |
c->add({it, std::next(it)}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename ForwardIterator> | |
void stable_sort_forward(ForwardIterator first, ForwardIterator last) | |
{ | |
using Range = std::pair<ForwardIterator, ForwardIterator>; | |
using Counter = binary_counter<merger<ForwardIterator>, Range>; | |
Counter c { merger<ForwardIterator>{}, { last, last } }; | |
for (; first != last; ++first) { | |
c.add(std::make_pair(first, std::next(first))); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Tree a | |
= EmptyTree | |
| Tree { root :: Node a } | |
data Node a | |
= Node { | |
value :: a, | |
children :: [Node a] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
treeWalkR :: Tree a -> [a] | |
treeWalkR EmptyTree = [] | |
treeWalkR (Tree root) = treeWalkR' root | |
treeWalkR' :: Node a -> [a] | |
treeWalkR' (Node v children) = v : concatMap treeWalkR' children |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
treeWalkH :: Tree a -> [a] | |
treeWalkH EmptyTree = [] | |
treeWalkH (Tree root) = treeWalkH' root | |
treeWalkH' :: Node a -> [a] | |
treeWalkH' = reverse . loop [] [] | |
where | |
loop out [] (Node v []) = v:out -- End of traversal | |
loop out stack (Node v (c:cs)) = loop (v:out) (toStack (cs:stack)) c | |
loop out (h:t) (Node v []) = loop (v:out) (toStack (tail h:t)) (head h) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
treeWalkC :: Tree a -> [a] | |
treeWalkC EmptyTree = [] | |
treeWalkC (Tree root) = treeWalkC' root | |
treeWalkC' :: Node a -> [a] | |
treeWalkC' n = loop n id | |
where | |
loop (Node v cs) cont = loopChildren cs (cont . (v:)) | |
loopChildren [] cont = cont [] | |
loopChildren (c:cs) cont = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
treeWalkM :: Tree a -> [a] | |
treeWalkM EmptyTree = [] | |
treeWalkM (Tree root) = treeWalkM' root | |
treeWalkM' :: Node a -> [a] | |
treeWalkM' n = runCont (loop n) id | |
where | |
loop (Node v cs) = do | |
rs <- mapM loop cs | |
return (v : concat rs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fizzBuzz :: Int -> String | |
fizzBuzz 0 = "0" | |
fizzBuzz n = | |
let res = fizzBuzzImpl [newRule 3 "Fizz", newRule 5 "Buzz"] n | |
in if res == "" | |
then show n | |
else res | |
type Rule = Int -> String |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testCases :: [(Int, String)] | |
testCases = [ (0, "0"), (1, "1"), (3, "Fizz"), (5, "Buzz") | |
, (7, "7"), (15, "FizzBuzz"), (100, "Buzz")] | |
tests :: Test | |
tests = TestList $ map createTestCase testCases | |
where | |
createTestCase (input, expected) = | |
let label = "FizzBuzz of " ++ show input | |
in TestCase $ assertEqual label expected (fizzBuzz input) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newtype Fizz = Fizz Int deriving (Show) | |
newtype Buzz = Buzz Int deriving (Show) | |
newtype Other = Other Int deriving (Show) | |
instance Arbitrary Fizz where | |
arbitrary = do | |
x <- (arbitrary `suchThat` (\n -> fizzBuzz n == "Fizz")) | |
return (Fizz x) | |
instance Arbitrary Buzz where |