Skip to content

Instantly share code, notes, and snippets.

tests :: Test
tests =
let multiples_of_3_and_5 = [3 * 5, 2 * 3 * 5, 3 * 3 * 5]
multiples_of_3 = [3, 1 * 3, 2 * 3, 3 * 3]
multiples_of_5 = [5, 1 * 5, 2 * 5, 3 * 5]
other_numbers = [0, 1, 2, 4]
in TestList
[ fizzBuzz `should_equal` "FizzBuzz" `for_all` multiples_of_3_and_5
, fizzBuzz `should_equal` "Fizz" `for_all` multiples_of_3
, fizzBuzz `should_equal` "Buzz" `for_all` multiples_of_5
should_equal :: (Show t, Show a, Eq a) => (t -> a) -> a -> t -> Test
should_equal underTest expected input =
let label = "Expects " ++ show expected ++ " for " ++ show input
in TestCase $ assertEqual label expected (underTest input)
should_do_like :: (Show t, Show a, Eq a) => (t -> a) -> (t -> a) -> t -> Test
should_do_like underTest refFct input
= should_equal underTest (refFct input) input
for_all :: (a -> Test) -> [a] -> Test
struct batch_options
{
bool m_with_minimum;
bool m_with_average;
bool m_with_variations;
double m_limit;
std::function<bool (stock_update const&)> m_filter;
};
struct batch_result
batch_result res = terrible_design(source, batch_options {
true, true, true, 2.0,
[](stock_update const& m) { return m.m_id == "A"; }
});
template<typename Filter>
auto read_stock_filtered_by(Filter filter) {
return [filter] (auto const& source) {
return source
| transformed(parse_message)
| filtered(filter)
| transformed(to(&stock::m_value));
};
}
auto read_stock_by_id(std::string const& id) {
return read_stock_filtered_by(equaling(id, on(&stock_update::m_id)));
}
//Assembling the algorithm, the source and the pipeline
std::vector<std::string> fake_source = ... ;
auto result = algorithm(read_stock_by_id("EUR")(fake_source));
template<typename Range>
double extract_min_value(Range const& values);
template<typename Range>
double compute_average(Range const& values);
template<typename Range>
int count_big_variations(Range const& values);
auto read = read_stock_by_id("A");
double min_val = extract_min_value(read(source));
double avg_val = compute_average(read(source));
int variation_count = count_big_variations(read(source));
struct RunningAverage
{
double m_value;
int m_count;
};
struct GetAverage
{
RunningAverage operator()() const { return RunningAverage{ 0.0, 0 }; }
RunningAverage operator()(RunningAverage const& avg, double value) const
template<typename T>
struct FindMinimum
{
T operator()() const { return std::numeric_limits<T>::max(); }
T operator()(T const& current_min, T const& val) const
{
return std::min(current_min, val);
}
};