Skip to content

Commit

Permalink
Formula engine: New take_while function
Browse files Browse the repository at this point in the history
Returns all elements up to the first one failing the condition
  • Loading branch information
CelticMinstrel committed Mar 18, 2016
1 parent 390baf7 commit 159dbcf
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/formula_function.cpp
Expand Up @@ -828,6 +828,25 @@ class map_function : public function_expression {
}
};

class take_while_function : public function_expression {
public:
explicit take_while_function(const args_list& args)
: function_expression("take_while", args, 2, 2)
{}
private:
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
const variant& items = args()[0]->evaluate(variables, fdb);
variant_iterator it = items.begin();
for(; it != items.end(); ++it) {
const variant matches = args().back()->evaluate(formula_variant_callable_with_backup(*it, variables),fdb);
if (!matches.as_bool())
break;
}
std::vector<variant> result(items.begin(), it);
return variant(&result);
}
};

class zip_function : public function_expression {
public:
explicit zip_function(const args_list& args)
Expand Down Expand Up @@ -1294,6 +1313,7 @@ functions_map& get_functions_map() {
FUNCTION(find);
FUNCTION(map);
FUNCTION(zip);
FUNCTION(take_while);
FUNCTION(reduce);
FUNCTION(sum);
FUNCTION(head);
Expand Down

0 comments on commit 159dbcf

Please sign in to comment.