Commit a78a72f3 authored by Chris Toshok's avatar Chris Toshok

zip() takes varargs. django passes 3 containers to it.

parent 3d9ead49
......@@ -554,19 +554,36 @@ Box* filter2(Box* f, Box* container) {
return rtn;
}
Box* zip2(Box* container1, Box* container2) {
Box* zip(BoxedTuple* containers) {
assert(containers->cls == tuple_cls);
BoxedList* rtn = new BoxedList();
if (containers->size() == 0)
return rtn;
llvm::iterator_range<BoxIterator> range1 = container1->pyElements();
llvm::iterator_range<BoxIterator> range2 = container2->pyElements();
std::vector<llvm::iterator_range<BoxIterator>> ranges;
for (auto container : *containers) {
ranges.push_back(container->pyElements());
}
BoxIterator it1 = range1.begin();
BoxIterator it2 = range2.begin();
std::vector<BoxIterator> iterators;
for (auto range : ranges) {
iterators.push_back(range.begin());
}
for (; it1 != range1.end() && it2 != range2.end(); ++it1, ++it2) {
listAppendInternal(rtn, BoxedTuple::create({ *it1, *it2 }));
while (true) {
for (int i = 0; i < iterators.size(); i++) {
if (iterators[i] == ranges[i].end())
return rtn;
}
auto el = BoxedTuple::create(iterators.size());
for (int i = 0; i < iterators.size(); i++) {
el->elts[i] = *iterators[i];
++(iterators[i]);
}
listAppendInternal(rtn, el);
}
return rtn;
}
static Box* callable(Box* obj) {
......@@ -1170,7 +1187,8 @@ void setupBuiltins() {
{ NULL }));
builtins_module->giveAttr("filter",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)filter2, LIST, 2), "filter"));
builtins_module->giveAttr("zip", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)zip2, LIST, 2), "zip"));
builtins_module->giveAttr(
"zip", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)zip, LIST, 0, 0, true, false), "zip"));
builtins_module->giveAttr(
"dir", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)dir, LIST, 1, 1, false, false), "dir", { NULL }));
builtins_module->giveAttr("vars", new BoxedBuiltinFunctionOrMethod(
......
......@@ -27,7 +27,9 @@ class C(object):
print sum([C(1), C(2), C(3)], C(4)).n
print zip()
print zip([1, 2, 3, 0], ["one", "two", "three"])
print zip([1, 2, 3, 0], ["one", "two", "three"], ["uno", "dos", "tres", "quatro"])
print filter(lambda x: x % 2, xrange(20))
print type(enumerate([]))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment