diff options
author | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2025-03-24 13:40:14 +0100 |
---|---|---|
committer | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2025-03-24 16:28:48 +0100 |
commit | ed2a48615e568a59409fc0dd7d7341d27af896ca (patch) | |
tree | a5cf36f11d3027043e00b77c35e1728e3c5c37c0 | |
parent | 9284b926199efabb66fc1e39b11c2e054870ba61 (diff) | |
download | justbuild-ed2a48615e568a59409fc0dd7d7341d27af896ca.tar.gz |
Add 'zip_map' expression
Produces the mapping between correspondingly indexed entries of two
lists, one containing the keys and the other of values. The keys
list must have string entries. If the two input lists are of
different sizes, the extra elements are ignored.
-rw-r--r-- | src/buildtool/build_engine/expression/evaluator.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/buildtool/build_engine/expression/evaluator.cpp b/src/buildtool/build_engine/expression/evaluator.cpp index 3f166b36..3283c2b1 100644 --- a/src/buildtool/build_engine/expression/evaluator.cpp +++ b/src/buildtool/build_engine/expression/evaluator.cpp @@ -1064,6 +1064,29 @@ auto ZipWithExpr(SubExprEvaluator&& eval, return ExpressionPtr{result}; } +auto ZipMapExpr(SubExprEvaluator&& eval, + ExpressionPtr const& expr, + Configuration const& env) -> ExpressionPtr { + auto range_key = eval(expr->Get("range_key", Expression::kEmptyList), env); + auto const& range_key_list = range_key->List(); + if (range_key_list.empty()) { + return Expression::kEmptyMap; + } + auto range_val = eval(expr->Get("range_val", Expression::kEmptyList), env); + auto const& range_val_list = range_val->List(); + if (range_val_list.empty()) { + return Expression::kEmptyMap; + } + auto result = Expression::map_t::underlying_map_t{}; + for (auto it = + std::make_pair(range_key_list.begin(), range_val_list.begin()); + it.first != range_key_list.end() and it.second != range_val_list.end(); + ++it.first, ++it.second) { + result[(*it.first)->String()] = *it.second; + } + return ExpressionPtr{Expression::map_t{result}}; +} + auto FoldLeftExpr(SubExprEvaluator&& eval, ExpressionPtr const& expr, Configuration const& env) -> ExpressionPtr { @@ -1361,6 +1384,7 @@ auto const kBuiltInFunctions = {"foreach", ForeachExpr}, {"foreach_map", ForeachMapExpr}, {"zip_with", ZipWithExpr}, + {"zip_map", ZipMapExpr}, {"foldl", FoldLeftExpr}, {"let*", LetExpr}, {"env", EnvExpr}, |