Skip to content

Commit

Permalink
Formula engine: add tan and tail functions, extra argument to head fu…
Browse files Browse the repository at this point in the history
…nction
  • Loading branch information
CelticMinstrel committed Mar 18, 2016
1 parent e4b944d commit 1c9eb0c
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions src/formula_function.cpp
Expand Up @@ -498,6 +498,19 @@ class cos_function
}
};

class tan_function
: public function_expression {
public:
explicit tan_function(const args_list& args)
: function_expression("tan", args, 1, 1)
{}
private:
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
const double angle = args()[0]->evaluate(variables,fdb).as_decimal() / 1000.0;
return variant(1000.0 * tan(angle * pi<double>() / 180.0), variant::DECIMAL_VARIANT);
}
};

class index_of_function : public function_expression {
public:
explicit index_of_function(const args_list& args)
Expand Down Expand Up @@ -862,7 +875,7 @@ class sum_function : public function_expression {
class head_function : public function_expression {
public:
explicit head_function(const args_list& args)
: function_expression("head", args, 1, 1)
: function_expression("head", args, 1, 2)
{}
private:
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
Expand All @@ -871,7 +884,40 @@ class head_function : public function_expression {
if(it == items.end()) {
return variant();
}
return *it;
if(args().size() == 1) {
return *it;
}
const int n = items.num_elements(), req = args()[1]->evaluate(variables,fdb).as_int();
const int count = req < 0 ? n - std::min(-req, n) : std::min(req, n);
variant_iterator end = it;
std::advance(end, count);
std::vector<variant> res;
std::copy(it, end, std::back_inserter(res));
return variant(&res);
}
};

class tail_function : public function_expression {
public:
explicit tail_function(const args_list& args)
: function_expression("tail", args, 1, 2)
{}
private:
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
const variant items = args()[0]->evaluate(variables,fdb);
variant_iterator it = items.end();
if(it == items.begin()) {
return variant();
}
if(args().size() == 1) {
return *--it;
}
const int n = items.num_elements(), req = args()[1]->evaluate(variables,fdb).as_int();
const int count = req < 0 ? n - std::min(-req, n) : std::min(req, n);
std::advance(it, -count);
std::vector<variant> res;
std::copy(it, items.end(), std::back_inserter(res));
return variant(&res);
}
};

Expand Down Expand Up @@ -1168,6 +1214,7 @@ functions_map& get_functions_map() {
FUNCTION(reduce);
FUNCTION(sum);
FUNCTION(head);
FUNCTION(tail);
FUNCTION(size);
FUNCTION(null);
FUNCTION(ceil);
Expand All @@ -1186,6 +1233,7 @@ functions_map& get_functions_map() {
FUNCTION(concatenate);
FUNCTION(sin);
FUNCTION(cos);
FUNCTION(tan);
#undef FUNCTION
}

Expand Down

0 comments on commit 1c9eb0c

Please sign in to comment.