def eq(x,y):
"""
My work around for discrepency in bool equality True==1 and False==0.
-
>>> eq(true, 1)
false
-
>>> eq(1, 1.0)
true
"""
def range(*x):
return todyna(_range(*x))
-
def split(s, delim='\s+'):
return todyna(re.split(delim, s))
-
def crash():
class Crasher(Exception): pass
raise Crasher('Hey, you asked for it!')
-
def pycall(name, *args):
"""
Temporary foreign function interface - call Python functions from dyna!
x = eval(name)(*args)
return todyna(x)
-
def topython(x):
-
if islist(x):
return [topython(y) for y in x.aslist]
-
elif isinstance(x, MapsTo):
return tuple(x.args)
-
return x
-
def todyna(x):
-
if isinstance(x, (set, Counter)):
x = list(x)
x.sort()
return todyna(x)
-
elif x is True:
return true
-
elif x is False:
return false
-
elif isinstance(x, dict):
return todyna([MapsTo(todyna(k), todyna(v)) for k,v in x.items()])
-
elif isinstance(x, (list, tuple)):
c = Nil
for y in reversed(x):
c = Cons(todyna(y), c)
return c
else:
-
return x
-
def get(x, i):
return x[i]
-
def getkey(m, k):
return m[k]
-
def setkey(m, k, v):
m[k] = v
return m
-
def islist(x):
return isinstance(x, Cons) or x is Nil
-
def iter_cons(x):
if not islist(x):
raise TypeError("Attemping to iterate something which isn't a list. %r" % (x,))
return x.like_chart()
-
def in_list(x, a):
if not islist(a):
raise TypeError("Attemping to iterate something which isn't a list. %r" % (a,))
return todyna(x in a.aslist)
-
# should probably be done with memoized backchaining...
def read_lines(filename):
with file(filename) as f:
return x is true or x is false
-
-
def _repr(x):
-
-# TODO: this assertion should eventually hold.
-# assert x is not True and x is not False, x
- if x is True:
- return 'true'
- elif x is False:
- return 'false'
- elif x is None:
+ #assert x is not True and x is not False, x
+ if x is None:
return 'null'
elif isinstance(x, basestring):
# dyna doesn't accept single-quoted strings