From d3a373ce941b0bf0c2e991ec337e64975689ccd3 Mon Sep 17 00:00:00 2001 From: VaclavT Date: Sun, 2 Jan 2022 12:44:49 +0100 Subject: [PATCH] rewrites to for loop --- ml.cpp | 78 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/ml.cpp b/ml.cpp index e2b42fc..0362f7e 100644 --- a/ml.cpp +++ b/ml.cpp @@ -85,8 +85,8 @@ MlValue::MlValue(const std::vector &list) : type(LIST), list(list) {} MlValue::MlValue(const std::vector &slist) : type(LIST) { list.reserve(slist.size()); - for (size_t i = 0; i < slist.size(); i++) - list.push_back(MlValue::string(slist[i])); + for (const auto & item : slist) + list.push_back(MlValue::string(item)); } MlValue MlValue::quote(const MlValue "ed) { @@ -125,15 +125,15 @@ MlValue MlValue::nil() { MlValue::MlValue(const std::vector ¶ms, MlValue ret, MlEnvironment const &env) : type(LAMBDA) { // We store the params and the result in the list member // instead of having dedicated members. This is to save memory. - list.push_back(MlValue(params)); + list.emplace_back(params); list.push_back(ret); -// // Lambdas capture only variables that they know they will use. + // Lambdas capture only variables that they know they will use. std::vector used_atoms = ret.get_used_atoms(); - for (size_t i = 0; i < used_atoms.size(); i++) { + for (const auto & used_atom : used_atoms) { // If the environment has a symbol that this lambda uses, capture it. - if (env.has(used_atoms[i])) - lambda_scope.set(used_atoms[i], env.get(used_atoms[i])); + if (env.has(used_atom)) + lambda_scope.set(used_atom, env.get(used_atom)); } } @@ -167,9 +167,9 @@ std::vector MlValue::get_used_atoms() { case LIST: // If this is a list, add each of the atoms used in all // of the elements in the list. - for (size_t i = 0; i < list.size(); i++) { + for (auto & item : list) { // Get the atoms used in the element - tmp = list[i].get_used_atoms(); + tmp = item.get_used_atoms(); // Add the used atoms to the current list of used atoms result.insert(result.end(), tmp.begin(), tmp.end()); } @@ -226,7 +226,7 @@ std::vector MlValue::as_list() const { } // Push an item to the end of this list -void MlValue::push(MlValue val) { +void MlValue::push(const MlValue& val) { // If this item is not a list, you cannot push to it. if (type != LIST) throw MlError(*this, MlEnvironment(), MISMATCHED_TYPES); @@ -400,8 +400,8 @@ MlValue MlValue::operator+(const MlValue &other) const { // Maintain the value that will be returned MlValue result = *this; // Add each item in the other list to the end of this list - for (size_t i = 0; i < other.list.size(); i++) - result.push(other.list[i]); + for (const auto & item : other.list) + result.push(item); return result; } else throw MlError(*this, MlEnvironment(), INVALID_BIN_OP); @@ -584,9 +584,9 @@ std::string MlValue::debug() const { case FLOAT: return std::to_string(stack_data.f); case STRING: - for (size_t i = 0; i < str.length(); i++) { - if (str[i] == '"') result += "\\\""; - else result.push_back(str[i]); + for (char c : str) { + if (c == '"') result += "\\\""; + else result.push_back(c); } return "\"" + result + "\""; case LAMBDA: @@ -919,8 +919,8 @@ namespace builtin { // their arguments. To make a regular builtin that evaluates its // arguments, we just call this function in our builtin definition. void eval_args(std::vector &args, MlEnvironment &env) { - for (size_t i = 0; i < args.size(); i++) - args[i] = args[i].eval(env); + for (auto & arg : args) + arg = arg.eval(env); } // Create a lambda function (SPECIAL FORM) @@ -1018,8 +1018,8 @@ MlValue for_loop(std::vector args, MlEnvironment &env) { MlValue acc; std::vector list = args[1].eval(env).as_list(); - for (size_t i = 0; i < list.size(); i++) { - env.set(args[0].as_atom(), list[i]); + for (auto & item : list) { + env.set(args[0].as_atom(), item); for (size_t j = 2; j < args.size() - 1; j++) args[j].eval(env); @@ -1059,8 +1059,8 @@ MlValue do_and(std::vector args, MlEnvironment &env) { if (args.size() < 2) throw MlError(MlValue("and", do_and), env, TOO_FEW_ARGS); - for (size_t i = 0; i < args.size(); i++) - if (!args[i].eval(env).as_bool()) return MlValue::nil(); + for (auto & arg : args) + if (!arg.eval(env).as_bool()) return MlValue::nil(); return MlValue{true}; } @@ -1070,8 +1070,8 @@ MlValue do_or(std::vector args, MlEnvironment &env) { if (args.size() < 2) throw MlError(MlValue("or", do_or), env, TOO_FEW_ARGS); - for (size_t i = 0; i < args.size(); i++) - if (args[i].eval(env).as_bool()) return MlValue{true}; + for (auto & arg : args) + if (arg.eval(env).as_bool()) return MlValue{true}; return MlValue::nil(); } @@ -1739,8 +1739,8 @@ MlValue string_regex_list(std::vector args, MlEnvironment &env) { auto found_matches = regexp_search2(args[0].as_string(), args[1].as_string(), match_mode, ignore_case); std::vector list; - for(auto &l : found_matches) { - list.push_back(l); + for(auto &item : found_matches) { + list.push_back(item); } return MlValue(list); } @@ -1859,9 +1859,9 @@ MlValue map_list(std::vector args, MlEnvironment &env) { if (args.size() != 2) throw MlError(MlValue("map_list", map_list), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); - std::vector result, l = args[1].as_list(), tmp; - for (size_t i = 0; i < l.size(); i++) { - tmp.push_back(l[i]); + std::vector result, list = args[1].as_list(), tmp; + for (const auto & item : list) { + tmp.push_back(item); result.push_back(args[0].apply(tmp, env)); tmp.clear(); } @@ -1876,11 +1876,11 @@ MlValue filter_list(std::vector args, MlEnvironment &env) { if (args.size() != 2) throw MlError(MlValue("filter_list", filter_list), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); - std::vector result, l = args[1].as_list(), tmp; - for (size_t i = 0; i < l.size(); i++) { - tmp.push_back(l[i]); + std::vector result, list = args[1].as_list(), tmp; + for (const auto & item : list) { + tmp.push_back(item); if (args[0].apply(tmp, env).as_bool()) - result.push_back(l[i]); + result.push_back(item); tmp.clear(); } return MlValue(result); @@ -1894,11 +1894,11 @@ MlValue reduce_list(std::vector args, MlEnvironment &env) { if (args.size() != 3) throw MlError(MlValue("reduce_list", reduce_list), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS); - std::vector l = args[2].as_list(), tmp; + std::vector list = args[2].as_list(), tmp; MlValue acc = args[1]; - for (size_t i = 0; i < l.size(); i++) { + for (const auto & item : list) { tmp.push_back(acc); - tmp.push_back(l[i]); + tmp.push_back(item); acc = args[0].apply(tmp, env); tmp.clear(); } @@ -1951,8 +1951,8 @@ MlValue benchmark(std::vector args, MlEnvironment &env) { MlValue thread_create(std::vector args, MlEnvironment &env) { auto functor = [](std::vector args, MlEnvironment &env) -> void { try { - for (size_t i = 0; i < args.size(); i++) - MlValue acc = args[i].eval(env); + for (auto & arg : args) + MlValue acc = arg.eval(env); } catch (const MlError &e) { std::cerr << "thread_create exception: " << e.description() << std::endl; } catch (const std::exception &e) { @@ -2327,8 +2327,8 @@ int main(int argc, char *argv[]) { // passed code if (cmdOptionExists(argv, argv + argc, "-c")) { std::vector codes = getCmdOption(argv, argc, "-c"); - for (size_t i = 0; i < codes.size(); i++) - run(codes[i], env); + for (auto & code : codes) + run(code, env); // run files } else if (cmdOptionExists(argv, argv + argc, "-f")) { for (auto & file : getCmdOption(argv, argc, "-f"))