forked from beeware/voc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request beeware#525 from ivanistheone/builtin_filter
add filter builtin
- Loading branch information
Showing
3 changed files
with
50 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.python.types; | ||
|
||
public class Filter extends org.python.types.Object implements org.python.Iterable { | ||
private org.python.Callable callable; | ||
private org.python.Iterable iterator; | ||
|
||
public Filter(org.python.Object callable, org.python.Iterable iterator) { | ||
if (org.python.types.NoneType.NONE == callable) { | ||
this.callable = null; | ||
} else { | ||
this.callable = (org.python.Callable) callable; | ||
} | ||
this.iterator = iterator; | ||
} | ||
|
||
@org.python.Method( | ||
__doc__ = "Implement iter(self)." | ||
) | ||
public org.python.Iterable __iter__() { | ||
return this; | ||
} | ||
|
||
@org.python.Method( | ||
__doc__ = "Implement next(self)." | ||
) | ||
public org.python.Object __next__() { | ||
while (true) { // loop until we find first true | ||
org.python.Object current = iterator.__next__(); | ||
org.python.Object value = current; | ||
if (callable != null) { | ||
org.python.Object[] args = new org.python.Object[] {current}; | ||
value = callable.invoke(args, new java.util.HashMap()); | ||
} | ||
if (value.toBoolean()) { | ||
return current; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters