Commit a6aee748 authored by Marius Wachtler's avatar Marius Wachtler

Implement all(iterable), any(iterable) and nonzero check for 'None'

parent 6d8b2e91
......@@ -49,6 +49,24 @@ extern "C" Box* abs_(Box* x) {
}
}
extern "C" Box* all(Box* container) {
for (Box* e : container->pyElements()) {
if (!nonzero(e)) {
return boxBool(false);
}
}
return boxBool(true);
}
extern "C" Box* any(Box* container) {
for (Box* e : container->pyElements()) {
if (nonzero(e)) {
return boxBool(true);
}
}
return boxBool(false);
}
extern "C" Box* min1(Box* container) {
Box* minElement = nullptr;
for (Box* e : container->pyElements()) {
......@@ -292,6 +310,9 @@ void setupBuiltins() {
builtins_module->giveAttr("NotImplemented", NotImplemented);
builtins_module->giveAttr("NotImplementedType", notimplemented_cls);
builtins_module->giveAttr("all", new BoxedFunction(boxRTFunction((void*)all, BOXED_BOOL, 1, false)));
builtins_module->giveAttr("any", new BoxedFunction(boxRTFunction((void*)any, BOXED_BOOL, 1, false)));
repr_obj = new BoxedFunction(boxRTFunction((void*)repr, NULL, 1, false));
builtins_module->giveAttr("repr", repr_obj);
len_obj = new BoxedFunction(boxRTFunction((void*)len, NULL, 1, false));
......
......@@ -933,6 +933,8 @@ extern "C" bool nonzero(Box* obj) {
rewriter->commit();
}
return static_cast<BoxedFloat*>(obj)->d != 0;
} else if (obj->cls == none_cls) {
return false;
}
// FIXME we have internal functions calling this method;
......
......@@ -11,3 +11,6 @@ print __builtins__
__builtins__ = 2
print __builtins__
print all([]), all([True]), all([False]), all([None]), all([True, False, None])
print any([]), any([True]), any([False]), any([None]), any([True, False, None])
......@@ -6,3 +6,5 @@ f(1)
f(-1)
f(True)
f(False)
print None
print not None
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