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
struct VariationCount | |
{ | |
boost::optional<double> m_initial; | |
int m_count; | |
}; | |
struct CountVariationBiggerThan | |
{ | |
CountVariationBiggerThan(double limit) : m_limit(limit) {} | |
double m_limit; |
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 Range, typename... Steps> | |
auto fold(Range const& rng, Steps... steps) | |
{ | |
return std::accumulate( | |
rng.begin(), rng.end(), | |
std::make_tuple(steps()...), | |
par_steps(steps...)); | |
} |
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
auto read = read_stock_by_id("A"); | |
auto res = fold(read(source), | |
FindMinimum<double>(), | |
GetAverage(), | |
CountVariationBiggerThan(2)); |
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
auto res = accumulate( | |
read(source), | |
std::make_tuple( | |
FindMinimum<double>()(), | |
GetAverage()(), | |
CountVariationBiggerThan(2)()), | |
par_steps( | |
FindMinimum<double>(), | |
GetAverage(), | |
CountVariationBiggerThan(2))); |
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
auto to_nice_result = [](auto min, auto avg, auto big) { | |
return batch_result{ min, avg.m_value, big.m_count }; | |
}; | |
auto typed = apply(to_nice_result, fold_result); | |
std::cout | |
<< "Result summary:\n" | |
<< " - Minimum: " << typed.m_minimum << "\n" | |
<< " - Average: " << typed.m_average << "\n" | |
<< " - Variations: " << typed.m_variation_count << "\n"; |
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
type Id = String | |
data OpType = Add | Mul deriving (Show, Eq, Ord) | |
data Expr | |
= Cst Int | |
| Var Id | |
| Op OpType [Expr] | |
deriving (Show, Eq, Ord) |
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
cst = Cst | |
var = Var | |
add = Op Add | |
mul = Op Mul |
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
add [ cst(1) | |
, cst(2) | |
, mul [cst(0), var("x"), var("y")] | |
, mul [cst(1), var("y"), cst(2)] | |
, add [cst(0), var("x") ]] |
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
prn :: Expr -> String | |
prn (Cst n) = show n | |
prn (Var v) = v | |
prn (Op Add xs) = "(+ " ++ unwords (map prn xs) ++ ")" | |
prn (Op Mul xs) = "(* " ++ unwords (map prn xs) ++ ")" |
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
> let e = add [ cst(1) | |
, cst(2) | |
, mul [cst(0), var("x"), var("y")] | |
, mul [cst(1), var("y"), cst(2)] | |
, add [cst(0), var("x") ]] | |
> prn e | |
"(+ 1 2 (* 0 x y) (* 1 y 2) (+ 0 x))" |