[Checkins] SVN: Sandbox/adamg/ocql/trunk/src/ocql/ small fix for Reduce

Adam Groszer agroszer at gmail.com
Thu Aug 14 12:40:50 EDT 2008


Log message for revision 89841:
  small fix for Reduce

Changed:
  U   Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.py
  U   Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py
  U   Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.txt

-=-
Modified: Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.py	2008-08-14 16:16:20 UTC (rev 89840)
+++ Sandbox/adamg/ocql/trunk/src/ocql/compiler/compiler.py	2008-08-14 16:40:49 UTC (rev 89841)
@@ -140,14 +140,14 @@
     def __call__(self):
         if self.context.klass == set:
             return 'reduce(%s, map(%s, %s), %s)' % (
+                compile(self.context.aggreg),
                 compile(self.context.func),
-                compile(self.context.aggreg),
                 compile(self.context.coll),
                 compile(self.context.expr))
         elif self.context.klass == list:
             return 'reduce(%s, map(%s, %s), %s)'% (
+                compile(self.context.aggreg),
                 compile(self.context.func),
-                compile(self.context.aggreg),
                 compile(self.context.coll),
                 compile(self.context.expr))
 

Modified: Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py	2008-08-14 16:16:20 UTC (rev 89840)
+++ Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py	2008-08-14 16:40:49 UTC (rev 89841)
@@ -173,7 +173,7 @@
 
 
 class MakeFromIndex(BaseAlgebra):
-    
+
     implements(IMakeFromIndex)
 
     def __init__(self, coll1, coll2, expr1, expr2, operator, value):

Modified: Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.txt
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.txt	2008-08-14 16:16:20 UTC (rev 89840)
+++ Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.txt	2008-08-14 16:40:49 UTC (rev 89841)
@@ -8,8 +8,8 @@
 functional arguments. This treatment makes the operations more regular as
 variations can be captured in the functional arguments. Unlike methods found
 in the object-oriented paradigm, these functions are system-defined and hence
-amenable to reasoning and therefore optimisation. The use of functional 
-arguments together with a regular set of collection operations makes the 
+amenable to reasoning and therefore optimisation. The use of functional
+arguments together with a regular set of collection operations makes the
 algebra more extensible as a new collection can be integrated by providing a
 set of the regular operations. However, it should be noted that the canonical
 algebra is not a minimal set of operations, some operations can be defined by
@@ -35,25 +35,25 @@
 Union ( C1, C2 )
 -----------------
 The union operations combine two collections. The cardinality of each resultant
-element is the sum of its cardinalities in the operand collections except in 
-the case of sets where all elements are unique. 
+element is the sum of its cardinalities in the operand collections except in
+the case of sets where all elements are unique.
 Ordering, if respected, will be preserved.
 
-	>>> from ocql.rewriter.algebra import Union
-	>>> x = Union(set, set([1, 2]), set([2, 3]))
-	>>> x
-	Union(<type 'set'>, set([1, 2]), set([2, 3]))
+    >>> from ocql.rewriter.algebra import Union
+    >>> x = Union(set, set([1, 2]), set([2, 3]))
+    >>> x
+    Union(<type 'set'>, set([1, 2]), set([2, 3]))
 
-	>>> run(x)
-	set([1, 2, 3])
+    >>> run(x)
+    set([1, 2, 3])
 
 
-	>>> x = Union(list, [1, 2], [2, 3])
-	>>> x
-	Union(<type 'list'>, [1, 2], [2, 3])
+    >>> x = Union(list, [1, 2], [2, 3])
+    >>> x
+    Union(<type 'list'>, [1, 2], [2, 3])
 
-	>>> run(x)
-	[1, 2, 3]
+    >>> run(x)
+    [1, 2, 3]
 
 
 Differ ( C1, C2 )
@@ -64,51 +64,53 @@
 first operand collection and that in the second operand collection.
 Ordering, if respected, will be preserved.
 
-	>>> from ocql.rewriter.algebra import Differ
-	>>> x = Differ(set, set([1, 2]), set([2, 3]))
-	>>> x
-	Differ(<type 'set'>, set([1, 2]), set([2, 3]))
+    >>> from ocql.rewriter.algebra import Differ
+    >>> x = Differ(set, set([1, 2]), set([2, 3]))
+    >>> x
+    Differ(<type 'set'>, set([1, 2]), set([2, 3]))
 
-	>>> run(x)
-	set([1])
+    >>> run(x)
+    set([1])
 
-	>>> x = Differ(list, [1, 2], [2, 3])
-	>>> x
-	Differ(<type 'list'>, [1, 2], [2, 3])
+    >>> x = Differ(list, [1, 2], [2, 3])
+    >>> x
+    Differ(<type 'list'>, [1, 2], [2, 3])
 
-	>>> run(x)
-	[1]
+    >>> run(x)
+    [1]
 
 
 Equal ( E1, E2 )
 ----------------
-The equal operations compare two collections of the same kind and return true 
+The equal operations compare two collections of the same kind and return true
 if their elements are the same.
 Duplication and ordering, if respected, will be taken into account.
 
 
 Unary Operations
 ----------------
-The operations described below are unary in the sense that each takes a 
+The operations described below are unary in the sense that each takes a
 collection as one of the operands. Other operands include functions on
 the elements of the operand collection and functions over results returned
 by other operand functions.
 
 Reduce ( E0, F1, Faggregate, C)
 ---------------------------------
-The reduce operations are used to combine elements in a collection. If the 
+The reduce operations are used to combine elements in a collection. If the
 operand collection C is empty, E0 is returned. When the operand collection
 is not empty, F1 is applied to each element of C and the results are supplied
 pairwise to Faggregate which accumulates the results to give a single value.
-	>>> import operator
-	>>> from ocql.rewriter.algebra import Reduce
-	>>> x = Reduce(set, None, 'lambda y, z: y+z', 'operator.add', set([1, 2, 3, 4, 5]))
-	>>> x
-	Reduce(<type 'set'>, None, lambda y, z: y+z, operator.add, set([1, 2, 3, 4, 5]))
-	
-	>>> run(x)
-	
 
+    >>> import operator
+    >>> from ocql.rewriter.algebra import Reduce
+    >>> x = Reduce(set, 0, 'lambda y: y+1', 'operator.add', set([1, 2, 3, 4, 5]))
+    >>> x
+    Reduce(<type 'set'>, 0, lambda y: y+1, operator.add, set([1, 2, 3, 4, 5]))
+
+    >>> run(x)
+    20
+
+
 Map ( F, C)
 -----------
 The map operations apply the operand function F to each element in the operand
@@ -122,20 +124,20 @@
 F returns true. The resultant collection is of the same kind as the operand
 collection.
 
-	>>> from ocql.rewriter.algebra import Select
-	>>> x = Select(set, 'lambda z: z%2', set([1,2,3]))
-	>>> x
-	Select(<type 'set'>, lambda z: z%2, set([1, 2, 3]))
+    >>> from ocql.rewriter.algebra import Select
+    >>> x = Select(set, 'lambda z: z%2', set([1,2,3]))
+    >>> x
+    Select(<type 'set'>, lambda z: z%2, set([1, 2, 3]))
 
-	>>> run(x)
-	set([1, 3])	
+    >>> run(x)
+    set([1, 3])
 
-	>>> x = Select(list, 'lambda z: z%2', [1,2,3])
-	>>> x
-	Select(<type 'list'>, lambda z: z%2, [1, 2, 3])
+    >>> x = Select(list, 'lambda z: z%2', [1,2,3])
+    >>> x
+    Select(<type 'list'>, lambda z: z%2, [1, 2, 3])
 
-	>>> run(x)
-	[1, 3]	
+    >>> run(x)
+    [1, 3]
 
 
 Make ( C )
@@ -144,21 +146,21 @@
 kind to one of the three collection kinds. Conversion from bag or set to list
 is non-deterministic as an arbitrary order will be assigned to the elements.
 
-	>>> from ocql.rewriter.algebra import Make
-	>>> x = Make(set, set([1,2]), set([2,3]))
-	>>> x
-	Make(<type 'set'>, set([1, 2]), set([2, 3]))
+    >>> from ocql.rewriter.algebra import Make
+    >>> x = Make(set, set([1,2]), set([2,3]))
+    >>> x
+    Make(<type 'set'>, set([1, 2]), set([2, 3]))
 
-#	>>> run(x)
+#   >>> run(x)
 
-	>>> x = Make(list, [1,2], [2,3])
-	>>> x
-	Make(<type 'list'>, [1, 2], [2, 3])
+    >>> x = Make(list, [1,2], [2,3])
+    >>> x
+    Make(<type 'list'>, [1, 2], [2, 3])
 
 
 Index( C, E )
 -------------
-The index operation takes a list C and returns an element of the list at 
+The index operation takes a list C and returns an element of the list at
 position E.
 
 
@@ -170,54 +172,54 @@
 --------------
 The empty operations take a value and return an empty collection.
 
-	>>> from ocql.rewriter.algebra import Empty
-	>>> x = Empty(set)
-	>>> x
-	Empty(<type 'set'>)
+    >>> from ocql.rewriter.algebra import Empty
+    >>> x = Empty(set)
+    >>> x
+    Empty(<type 'set'>)
 
-	>>> run(x)
-	set([])
+    >>> run(x)
+    set([])
 
-	>>> x = Empty(list)
-	>>> x
-	Empty(<type 'list'>)
+    >>> x = Empty(list)
+    >>> x
+    Empty(<type 'list'>)
 
-	>>> run(x)
-	[]
+    >>> run(x)
+    []
 
 
 Single ( E )
 ------------
 The single operations take a value and return a collection containing that value.
 
-	>>> from ocql.rewriter.algebra import Single
-	>>> x = Single(set, 1)
-	>>> x
-	Single(<type 'set'>, 1)
+    >>> from ocql.rewriter.algebra import Single
+    >>> x = Single(set, 1)
+    >>> x
+    Single(<type 'set'>, 1)
 
-	>>> run(x)
-	set([1])
+    >>> run(x)
+    set([1])
 
-	>>> x = Single(list, 1)
-	>>> x
-	Single(<type 'list'>, 1)
+    >>> x = Single(list, 1)
+    >>> x
+    Single(<type 'list'>, 1)
 
-	>>> run(x)
-	[1]
+    >>> run(x)
+    [1]
 
 
 If( Econdition, Etrue, Efalse )
 -------------------------------
-The if operation is a control operation. If Econdition evaluates to true, the 
+The if operation is a control operation. If Econdition evaluates to true, the
 value of Etrue is returned, otherwise the value of Efalse is returned.
 
-	>>> from ocql.rewriter.algebra import If
-	>>> x = If(True, 2, 3)
-	>>> x
-	If(True, 2, 3)
+    >>> from ocql.rewriter.algebra import If
+    >>> x = If(True, 2, 3)
+    >>> x
+    If(True, 2, 3)
 
-	>>> run(x)
-	2
+    >>> run(x)
+    2
 
 
 And( E1, E2 )
@@ -233,17 +235,17 @@
 An empty collection is returned if the first operand expression is less than the
 second one.
 
-	>>> from ocql.rewriter.algebra import Range
-	>>> x = Range(set, 1, 4)
-	>>> x
-	Range(<type 'set'>, 1, 4)
+    >>> from ocql.rewriter.algebra import Range
+    >>> x = Range(set, 1, 4)
+    >>> x
+    Range(<type 'set'>, 1, 4)
 
-	>>> run(x)
-	set([1, 2, 3])
+    >>> run(x)
+    set([1, 2, 3])
 
-	>>> x = Range(list, 1, 4)
-	>>> x
-	Range(<type 'list'>, 1, 4)
+    >>> x = Range(list, 1, 4)
+    >>> x
+    Range(<type 'list'>, 1, 4)
 
-	>>> run(x)
-	[1, 2, 3]
+    >>> run(x)
+    [1, 2, 3]



More information about the Checkins mailing list