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
This commit is contained in:
parent
d97c0ec46c
commit
4e0f558c13
3 changed files with 114 additions and 1 deletions
47
sca-cpp/trunk/modules/wsgi/stream-test.py
Executable file
47
sca-cpp/trunk/modules/wsgi/stream-test.py
Executable file
|
@ -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"
|
||||
|
|
@ -21,8 +21,12 @@
|
|||
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=$?
|
||||
|
|
|
@ -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 == ():
|
||||
|
|
Loading…
Add table
Reference in a new issue