# HG changeset patch # User Paul Boddie # Date 1486071506 -3600 # Node ID ca18c51ae153be86f80f97c5333709a74dc6cf15 # Parent 05ef82f4028030763e70e37dea86bf451b845a06 Rearranged iteration-related functionality within a __builtins__ subpackage. diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/__init__.py --- a/lib/__builtins__/__init__.py Wed Feb 01 17:42:42 2017 +0100 +++ b/lib/__builtins__/__init__.py Thu Feb 02 22:38:26 2017 +0100 @@ -62,7 +62,6 @@ from __builtins__.float import float from __builtins__.int import int from __builtins__.span import range, slice, xrange -from __builtins__.iterator import itemiterator from __builtins__.list import list from __builtins__.long import long from __builtins__.none import None, NoneType @@ -79,7 +78,7 @@ from __builtins__.comparable import cmp, hash from __builtins__.identity import callable, id, isinstance, issubclass, repr from __builtins__.io import open, raw_input, print_ -from __builtins__.iterable import all, any, enumerate, filter, iter, len, map, max, min, reduce, reversed, sorted, sum, zip +from __builtins__.iteration import all, any, enumerate, filter, iter, len, map, max, min, reduce, reversed, sorted, sum, zip from __builtins__.namespace import dir, globals, locals, vars from __builtins__.numeric import abs, divmod, pow, round diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/dict.py --- a/lib/__builtins__/dict.py Wed Feb 01 17:42:42 2017 +0100 +++ b/lib/__builtins__/dict.py Thu Feb 02 22:38:26 2017 +0100 @@ -19,7 +19,7 @@ this program. If not, see . """ -from __builtins__.iterator import itemiterator +from __builtins__.iteration.iterator import itemiterator from __builtins__.span import _max from native import isinstance as _isinstance diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/iterable.py --- a/lib/__builtins__/iterable.py Wed Feb 01 17:42:42 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,189 +0,0 @@ -#!/usr/bin/env python - -""" -Iteration-related functions. - -Copyright (C) 2015, 2016 Paul Boddie - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 3 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -details. - -You should have received a copy of the GNU General Public License along with -this program. If not, see . -""" - -def all(iterable): - - "Return whether all of the elements provided by 'iterable' are true." - - for i in iterable: - if not i: - return False - - return True - -def any(iterable): - - "Return whether any of the elements provided by 'iterable' are true." - - for i in iterable: - if i: - return True - - return False - -def enumerate(iterable, start=0): - - """ - Iterate over 'iterable', obtaining items and combining them with position - information, producing a sequence containing tuples of the form - (position, item). The first position is indicated by 'start' (which is zero - by default) and each subsequent positions is incremented from the one - preceding it. - """ - - l = [] - pos = start - - for i in iterable: - l.append((pos, i)) - pos += 1 - - return l - -def filter(function, sequence): - - """ - Apply 'function' to each element in 'sequence', returning a sequence of all - elements for which the result of the function evaluated to a true value. - """ - - l = [] - for i in sequence: - if function(i): - l.append(i) - return l - -def iter(collection): - - "Implementation of iter without callable plus sentinel support." - - return collection.__iter__() - -def len(obj): - - "Implementation of len." - - return obj.__len__() - -def map(function, sequence): - - """ - Apply 'function' to each element of 'sequence' in turn, appending the result - to a new sequence containing all results. - """ - - l = [] - for i in sequence: - l.append(function(i)) - return l - -def max(args): - - "Implementation of max." - - highest = None - for arg in args: - if highest is None or arg > highest: - highest = arg - return highest - -def min(args): - - "Implementation of min." - - lowest = None - for arg in args: - if lowest is None or arg < lowest: - lowest = arg - return lowest - -_reduce_default = object() - -def reduce(function, sequence, initial=_reduce_default): - - """ - Using 'function', reduce the given 'sequence' to a single result. - - With no 'initial' value specified, the first two elements in the 'sequence' - are used with the function to produce an initial result. With an initial - result available, a subsequent result is computed by using the initial - result and the next element in the sequence with the function. - - All subsequent results are computed using the current result and the next - available element with the function. This continues for all remaining - elements until the end of the sequence is reached. - """ - - result = initial - - for i in sequence: - if result is _reduce_default: - result = i - else: - result = function(result, i) - - return result - -def reversed(sequence): - - "Return a reversed version of the given 'sequence'." - - return sequence[::-1] - -def sorted(iterable, cmp=None, key=None, reverse=False): pass - -def sum(sequence, start=0): - - "Sum the elements in 'sequence', adding to any indicated 'start' value." - - total = start - for i in sequence: - total += i - return total - -def zip(args): - - """ - Zip the given 'args' together, producing for each index position tuples - containing the values for that position from each of the 'args'. - """ - - result = [] - pos = 0 - - # Repeat until one of the arguments runs out of elements. - - while True: - l = [] - - # Visit each argument in turn, collecting elements in the given - # position. - - for arg in args: - try: - l.append(arg[pos]) - except IndexError: - return result - - result.append(tuple(l)) - pos += 1 - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/iteration/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/__builtins__/iteration/__init__.py Thu Feb 02 22:38:26 2017 +0100 @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +""" +Iteration-related functionality. + +Copyright (C) 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +from __builtins__.iteration.enumeration import enumerate +from __builtins__.iteration.core import iter, len +from __builtins__.iteration.functional import filter, map, reduce, zip +from __builtins__.iteration.quantifiers import all, any +from __builtins__.iteration.ordering import reversed, sorted +from __builtins__.iteration.summary import max, min, sum + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/iteration/core.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/__builtins__/iteration/core.py Thu Feb 02 22:38:26 2017 +0100 @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +""" +Core iteration-related functions. + +Copyright (C) 2015, 2016, 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +def iter(collection): + + "Implementation of iter without callable plus sentinel support." + + return collection.__iter__() + +def len(obj): + + "Implementation of len." + + return obj.__len__() + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/iteration/enumeration.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/__builtins__/iteration/enumeration.py Thu Feb 02 22:38:26 2017 +0100 @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +""" +Enumeration-related functions. + +Copyright (C) 2015, 2016, 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +def enumerate(iterable, start=0): + + """ + Iterate over 'iterable', obtaining items and combining them with position + information, producing a sequence containing tuples of the form + (position, item). The first position is indicated by 'start' (which is zero + by default) and each subsequent positions is incremented from the one + preceding it. + """ + + l = [] + pos = start + + for i in iterable: + l.append((pos, i)) + pos += 1 + + return l + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/iteration/functional.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/__builtins__/iteration/functional.py Thu Feb 02 22:38:26 2017 +0100 @@ -0,0 +1,101 @@ +#!/usr/bin/env python + +""" +Functional operations for iterators. + +Copyright (C) 2015, 2016, 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +def filter(function, sequence): + + """ + Apply 'function' to each element in 'sequence', returning a sequence of all + elements for which the result of the function evaluated to a true value. + """ + + l = [] + for i in sequence: + if function(i): + l.append(i) + return l + +def map(function, sequence): + + """ + Apply 'function' to each element of 'sequence' in turn, appending the result + to a new sequence containing all results. + """ + + l = [] + for i in sequence: + l.append(function(i)) + return l + +_reduce_default = object() + +def reduce(function, sequence, initial=_reduce_default): + + """ + Using 'function', reduce the given 'sequence' to a single result. + + With no 'initial' value specified, the first two elements in the 'sequence' + are used with the function to produce an initial result. With an initial + result available, a subsequent result is computed by using the initial + result and the next element in the sequence with the function. + + All subsequent results are computed using the current result and the next + available element with the function. This continues for all remaining + elements until the end of the sequence is reached. + """ + + result = initial + + for i in sequence: + if result is _reduce_default: + result = i + else: + result = function(result, i) + + return result + +def zip(args): + + """ + Zip the given 'args' together, producing for each index position tuples + containing the values for that position from each of the 'args'. + """ + + result = [] + pos = 0 + + # Repeat until one of the arguments runs out of elements. + + while True: + l = [] + + # Visit each argument in turn, collecting elements in the given + # position. + + for arg in args: + try: + l.append(arg[pos]) + except IndexError: + return result + + result.append(tuple(l)) + pos += 1 + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/iteration/iterator.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/__builtins__/iteration/iterator.py Thu Feb 02 22:38:26 2017 +0100 @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +""" +Iterator objects. + +Copyright (C) 2015, 2016 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +class itemiterator: + + "An iterator for objects providing item access." + + def __init__(self, l): + + "Initialise with the given list 'l'." + + self.l = l + self.i = 0 + + def next(self): + + "Return the next item." + + try: + value = self.l[self.i] + self.i += 1 + return value + except IndexError: + raise StopIteration() + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/iteration/ordering.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/__builtins__/iteration/ordering.py Thu Feb 02 22:38:26 2017 +0100 @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +""" +Ordering-related functions. + +Copyright (C) 2015, 2016, 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +def reversed(sequence): + + "Return a reversed version of the given 'sequence'." + + return sequence[::-1] + +def sorted(iterable, cmp=None, key=None, reverse=False): + + # NOTE: To be implemented. + + pass + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/iteration/quantifiers.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/__builtins__/iteration/quantifiers.py Thu Feb 02 22:38:26 2017 +0100 @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +""" +Quantifier functions. + +Copyright (C) 2015, 2016, 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +def all(iterable): + + "Return whether all of the elements provided by 'iterable' are true." + + for i in iterable: + if not i: + return False + + return True + +def any(iterable): + + "Return whether any of the elements provided by 'iterable' are true." + + for i in iterable: + if i: + return True + + return False + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/iteration/summary.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/__builtins__/iteration/summary.py Thu Feb 02 22:38:26 2017 +0100 @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +""" +Iterator summary functions. + +Copyright (C) 2015, 2016, 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +def max(args): + + "Implementation of max." + + highest = None + for arg in args: + if highest is None or arg > highest: + highest = arg + return highest + +def min(args): + + "Implementation of min." + + lowest = None + for arg in args: + if lowest is None or arg < lowest: + lowest = arg + return lowest + +def sum(sequence, start=0): + + "Sum the elements in 'sequence', adding to any indicated 'start' value." + + total = start + for i in sequence: + total += i + return total + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/iterator.py --- a/lib/__builtins__/iterator.py Wed Feb 01 17:42:42 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -#!/usr/bin/env python - -""" -Iterator objects. - -Copyright (C) 2015, 2016 Paul Boddie - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 3 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -details. - -You should have received a copy of the GNU General Public License along with -this program. If not, see . -""" - -class itemiterator: - - "An iterator for objects providing item access." - - def __init__(self, l): - - "Initialise with the given list 'l'." - - self.l = l - self.i = 0 - - def next(self): - - "Return the next item." - - try: - value = self.l[self.i] - self.i += 1 - return value - except IndexError: - raise StopIteration() - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/list.py --- a/lib/__builtins__/list.py Wed Feb 01 17:42:42 2017 +0100 +++ b/lib/__builtins__/list.py Thu Feb 02 22:38:26 2017 +0100 @@ -19,7 +19,7 @@ this program. If not, see . """ -from __builtins__.iterator import itemiterator +from __builtins__.iteration.iterator import itemiterator from __builtins__.sequence import sequence from native import list_append, list_concat, list_element, list_init, \ list_len, list_nonempty, list_setelement, list_setsize diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/set.py --- a/lib/__builtins__/set.py Wed Feb 01 17:42:42 2017 +0100 +++ b/lib/__builtins__/set.py Thu Feb 02 22:38:26 2017 +0100 @@ -3,7 +3,7 @@ """ Set objects. -Copyright (C) 2015, 2016 Paul Boddie +Copyright (C) 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -19,6 +19,8 @@ this program. If not, see . """ +from __builtins__.iteration.iterator import itemiterator + class frozenset: def __init__(self, iterable): pass diff -r 05ef82f40280 -r ca18c51ae153 lib/__builtins__/tuple.py --- a/lib/__builtins__/tuple.py Wed Feb 01 17:42:42 2017 +0100 +++ b/lib/__builtins__/tuple.py Thu Feb 02 22:38:26 2017 +0100 @@ -19,7 +19,7 @@ this program. If not, see . """ -from __builtins__.iterator import itemiterator +from __builtins__.iteration.iterator import itemiterator from __builtins__.sequence import hashable, sequence from native import list_element, list_init, list_len, list_setsize, \ list_setelement