fixes
This commit is contained in:
54
ml.cpp
54
ml.cpp
@@ -117,20 +117,6 @@ bool is_symbol(char ch) {
|
||||
return (isalpha(ch) || ispunct(ch)) && ch != '(' && ch != ')' && ch != '"' && ch != '\'';
|
||||
}
|
||||
|
||||
// std::regex int_underscored_regex("[0-9][0-9_]+[0-9]");
|
||||
std::regex int_regex("[0-9]+");
|
||||
std::regex double_regex("[0-9]+\\.[0-9]+");
|
||||
|
||||
// Is string representing int value
|
||||
bool is_string_int(const std::string &str) {
|
||||
return std::regex_match(str, int_regex);
|
||||
}
|
||||
|
||||
// Is string representing float value
|
||||
bool is_string_float(const std::string &str) {
|
||||
return std::regex_match(str, double_regex);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// LISP CONSTRUCTS ////////////////////////////////////////////////////////////
|
||||
@@ -1148,30 +1134,7 @@ namespace builtin {
|
||||
std::vector<std::vector<std::string> > parsed_data; // TODO some default size here
|
||||
csv.parseCSV(args[0].as_string(), parsed_data);
|
||||
|
||||
int rows = parsed_data.size();
|
||||
int cols = rows > 0 ? parsed_data[0].size() : 0;
|
||||
|
||||
std::vector<MlValue> result;
|
||||
|
||||
if (rows > 0 && cols > 0) {
|
||||
for (int r = 0; r < rows; r++) {
|
||||
std::vector<MlValue> row;
|
||||
for (int c = 0; c < cols; c++) {
|
||||
std::string value = parsed_data[r][c];
|
||||
if (is_string_int(value)) {
|
||||
row.push_back(MlValue(stoi(value)));
|
||||
}
|
||||
if (is_string_float(value)) {
|
||||
row.push_back(MlValue(std::stod(value)));
|
||||
} else {
|
||||
row.push_back(MlValue::string(value));
|
||||
}
|
||||
}
|
||||
result.push_back(row);
|
||||
}
|
||||
}
|
||||
|
||||
return MlValue(result);
|
||||
return csv.ivalualize(parsed_data);
|
||||
}
|
||||
|
||||
// Get the contents of a file
|
||||
@@ -1208,21 +1171,22 @@ namespace builtin {
|
||||
eval_args(args, env);
|
||||
|
||||
// PERF optimize it for memory usage and performance
|
||||
// TODO handle second parameter (headers)
|
||||
if (args.size() != 1)
|
||||
throw MlError(MlValue("read_url", read_url), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||
if (args.size() < 1 || args.size() > 2)
|
||||
throw MlError(MlValue("read_url", read_url), env, args.size() < 1 ? TOO_FEW_ARGS : TOO_MANY_ARGS);
|
||||
|
||||
std::unordered_map<std::string, std::string> headers = {};
|
||||
HttpClient client;
|
||||
|
||||
if (args.size() == 2) {
|
||||
// do magick here
|
||||
// for (auto i = map.begin(); i != map.end(); ++i) {
|
||||
// headers[i->first] = i->second.getString();
|
||||
// }
|
||||
for(const auto& pair_list: args[1].as_list()[0].as_list()) {
|
||||
// TODO check its 2 string elements list
|
||||
const auto& pair = pair_list.as_list();
|
||||
headers[pair[0].as_string()] = pair[1].as_string();
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<int, std::string> result = client.doGetRequest(args[0].as_string(), headers);
|
||||
// TODO add helper function for this
|
||||
std::vector<MlValue> lst;
|
||||
lst.push_back(MlValue(result.first));
|
||||
lst.push_back(MlValue::string(result.second));
|
||||
|
||||
Reference in New Issue
Block a user