summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2010-05-13 05:28:54 +0000
committerjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2010-05-13 05:28:54 +0000
commit4e0f558c13807745ed59eabffe2c834322b26a1d (patch)
tree372fd7b7425c24d9a5f7a68590f385ccad50b9ec
parentd97c0ec46c554920350403623d70938216b5a2b0 (diff)
Add simple implementation of Scheme-like lazy streams.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@943806 13f79535-47bb-0310-9956-ffa450edef68
-rwxr-xr-xsca-cpp/trunk/modules/wsgi/stream-test.py47
-rwxr-xr-xsca-cpp/trunk/modules/wsgi/util-test6
-rw-r--r--sca-cpp/trunk/modules/wsgi/util.py62
3 files changed, 114 insertions, 1 deletions
diff --git a/sca-cpp/trunk/modules/wsgi/stream-test.py b/sca-cpp/trunk/modules/wsgi/stream-test.py
new file mode 100755
index 0000000000..2cff038f1e
--- /dev/null
+++ b/sca-cpp/trunk/modules/wsgi/stream-test.py
@@ -0,0 +1,47 @@
+#!/usr/bin/python
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Test stream functions
+
+import unittest
+from util import *
+
+def testStream():
+
+ s = cons_stream(0, lambda: cons_stream(1, lambda: cons(2, ())))
+ assert len(s) == 3
+ assert car(s) == 0
+ assert cadr(s) == 1
+ assert len(cdr(s)) == 2
+ assert s[0] == 0
+ assert s[1] == 1
+ assert s[2] == 2
+ assert s[:1] == (0, 1)
+ assert s[:5] == (0, 1, 2)
+ assert s[2:5] == (2,)
+ assert s[4:5] == ()
+ assert s[0:] == (0, 1, 2)
+ assert (0, 1, 2) == s[0:]
+
+ return True
+
+if __name__ == "__main__":
+ print "Testing..."
+ testStream()
+ print "OK"
+
diff --git a/sca-cpp/trunk/modules/wsgi/util-test b/sca-cpp/trunk/modules/wsgi/util-test
index a1c54e80f1..cb4e2f6c8a 100755
--- a/sca-cpp/trunk/modules/wsgi/util-test
+++ b/sca-cpp/trunk/modules/wsgi/util-test
@@ -21,9 +21,13 @@
here=`readlink -f $0`; here=`dirname $here`
python_prefix=`cat $here/../python/python.prefix`
-$python_prefix/bin/python xml-test.py
+$python_prefix/bin/python stream-test.py
rc=$?
if [ "$rc" = "0" ]; then
+ $python_prefix/bin/python xml-test.py
+ rc=$?
+fi
+if [ "$rc" = "0" ]; then
$python_prefix/bin/python atom-test.py
rc=$?
fi
diff --git a/sca-cpp/trunk/modules/wsgi/util.py b/sca-cpp/trunk/modules/wsgi/util.py
index d945c7bdef..560101e32d 100644
--- a/sca-cpp/trunk/modules/wsgi/util.py
+++ b/sca-cpp/trunk/modules/wsgi/util.py
@@ -16,6 +16,7 @@
# under the License.
# Simple utility functions
+from sys import maxint
# Scheme-like lists
def cons(a, b):
@@ -23,10 +24,16 @@ def cons(a, b):
def car(l):
return l[0]
+
+def first(l):
+ return car(l)
def cdr(l):
return l[1:]
+def rest(l):
+ return cdr(l)
+
def cadr(l):
return car(cdr(l))
@@ -36,12 +43,17 @@ def cddr(l):
def caddr(l):
return car(cddr(l))
+def append(a, b):
+ return a + b
+
def reverse(l):
r = list(l)
r.reverse()
return tuple(r)
def isNil(l):
+ if isinstance(l, streampair):
+ return l.isNil()
return l == ()
def isSymbol(v):
@@ -57,6 +69,56 @@ def isList(v):
def isTaggedList(v, t):
return isList(v) and not isNil(v) and car(v) == t
+
+# Scheme-like streams
+class streampair(object):
+ def __init__(self, car, cdr):
+ self.car = car
+ self.cdr = cdr
+
+ def __repr__(self):
+ return repr(self[0:len(self)])
+
+ def isNil(self):
+ return self.cdr == ()
+
+ def __len__(self):
+ if self.cdr == ():
+ return 0
+ return 1 + len(self.cdr())
+
+ def __getitem__(self, i):
+ if i == 0:
+ return self.car
+ return self.cdr()[i - 1]
+
+ def __getslice__(self, i, j):
+ if isNil(self):
+ return ()
+ if i > 0:
+ if j == maxint:
+ return self.cdr()[i - 1: j]
+ return self.cdr()[i - 1: j - 1]
+ if j == maxint:
+ return self
+ if j == 0:
+ return (self.car,)
+ return (self.car,) + self.cdr()[: j - 1]
+
+ def __eq__(self, other):
+ sl = len(self)
+ ol = len(other)
+ if sl != ol:
+ return False
+ return self[0: sl] == other[0: ol]
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+def cons_stream(car, cdr):
+ return streampair(car, cdr)
+
+
# Scheme-like associations
def assoc(k, l):
if l == ():