-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathlazy_seq.py
More file actions
66 lines (53 loc) · 1.47 KB
/
lazy_seq.py
File metadata and controls
66 lines (53 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pixie.vm.object as object
from pixie.vm.primitives import nil
import pixie.vm.stdlib as proto
from pixie.vm.code import extend, as_var
import pixie.vm.rt as rt
import rpython.rlib.jit as jit
class LazySeq(object.Object):
_type = object.Type(u"pixie.stdlib.LazySeq")
def type(self):
return LazySeq._type
def __init__(self, fn, meta=nil):
self._fn = fn
self._meta = meta
self._s = nil
@jit.jit_callback("lazy_seq_sval")
def sval(self):
if self._fn is None:
return self._s
else:
self._s = self._fn.invoke([])
self._fn = None
return self._s
@jit.dont_look_inside
def lazy_seq_seq(self):
self.sval()
if self._s is not nil:
ls = self._s
while True:
if isinstance(ls, LazySeq):
ls = ls.sval()
continue
else:
self._s = ls
return rt.seq(self._s)
else:
return nil
@extend(proto._first, LazySeq)
def _first(self):
assert isinstance(self, LazySeq)
rt.seq(self)
return rt.first(self._s)
@extend(proto._next, LazySeq)
def _next(self):
assert isinstance(self, LazySeq)
rt.seq(self)
return rt.next(self._s)
@extend(proto._seq, LazySeq)
def _seq(self):
assert isinstance(self, LazySeq)
return self.lazy_seq_seq()
@as_var("lazy-seq*")
def lazy_seq(f):
return LazySeq(f)