second is implemented in c++ for performance reason

This commit is contained in:
VaclavT 2021-10-11 17:22:25 +02:00
parent c30432d175
commit 4a074e9ba3
1 changed files with 14 additions and 0 deletions

14
ml.cpp
View File

@ -1580,6 +1580,19 @@ MlValue tail(std::vector<MlValue> args, MlEnvironment &env) {
return MlValue(result);
}
MlValue second(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 1)
throw MlError(MlValue("second", second), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> list = args[0].as_list();
if (list.empty() || list.size() < 2)
throw MlError(MlValue("second", second), env, INDEX_OUT_OF_RANGE);
return list[1];
}
MlValue parse(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
@ -1998,6 +2011,7 @@ MlValue MlEnvironment::get(const std::string &name) const {
if (name == "head") return MlValue("head", builtin::head);
if (name == "tail") return MlValue("tail", builtin::tail);
if (name == "first") return MlValue("first", builtin::head);
if (name == "second") return MlValue("second", builtin::second);
if (name == "last") return MlValue("last", builtin::pop);
if (name == "range") return MlValue("range", builtin::range);