]> hydra-www.ietfng.org Git - dyna2/commitdiff
stable `set` and `bag` ordering.
authorTim Vieira <tim.f.vieira@gmail.com>
Tue, 2 Jul 2013 22:13:36 +0000 (18:13 -0400)
committerTim Vieira <tim.f.vieira@gmail.com>
Tue, 2 Jul 2013 22:13:36 +0000 (18:13 -0400)
examples/expected/lists.py.out
examples/lists.dyna
src/Dyna/Backend/Python/stdlib.py
src/Dyna/Backend/Python/term.py

index 98ad10adb74f44681833034cceb03f19545e359f..24c96b56edbf135e3179fd0583a5d6c2947744a7 100644 (file)
@@ -6,6 +6,8 @@ b = true.
 c = true.
 d = false.
 things = [1, [2, 2], [3, 4]].
+thingsbag = [1, 1, 2, "three"].
+thingset = [1, 2, "three"].
 
 d/1
 ===
index 9080c63ea057e49bb1c6ec96a88af9ede5b86f12..5e4dd94a6d9155c47f77d6afe0035760ebbc4480 100644 (file)
@@ -24,3 +24,13 @@ foo(A) := true for &f(A) in [1,2,&f("a"),3].
 
 % this one checks if the value of f(A) is in the list, (note: 1 == True, in python).
 goo(A) := true for f(A) in [1,2,&f("a"),3].
+
+thingsbag bag= "three".
+thingsbag bag= 1.
+thingsbag bag= 1.
+thingsbag bag= 2.
+
+thingset set= "three".
+thingset set= 1.
+thingset set= 1.
+thingset set= 2.
\ No newline at end of file
index 2f47eb2b3e0f29fed35a483b1a8280ad2a69c6d5..9c0d2d11483c817bcea18856f987807df066efcb 100644 (file)
@@ -1,5 +1,6 @@
 import re
 from term import Term, Cons, Nil
+from collections import Counter
 
 try:
     from numpy import log, exp, sqrt
@@ -27,7 +28,7 @@ def pycall(name, *args):
     return todyna(x)
 
 def todyna(x):
-    if isinstance(x, (set, list, tuple)):
+    if isinstance(x, (list, tuple, set, Counter)):
         return todynalist(x)
     return x
 
@@ -37,6 +38,10 @@ def topython(x):
     return x
 
 def todynalist(x):
+    if isinstance(x, (set, Counter)):
+        x = list(x)
+        x.sort()
+        return todynalist(x)
     return _todynalist(list(x))
 
 def _todynalist(x):
index 60f54e49c98b3728ada0d633a724215af1b3ef6c..fa2e0693a91d815640ed038d80e49d8834046356 100644 (file)
@@ -71,11 +71,11 @@ class Cons(Term):
             else:
                 yield a, (None,), a
 
-    def __eq__(self, other):
+    def __cmp__(self, other):
         try:
-            return self.aslist == other.aslist
+            return cmp(self.aslist, other.aslist)
         except AttributeError:
-            return False
+            return 1
 
 
 class _Nil(Term):