Repository navigation
Expand file tree
/
Copy pathlazyutil.py
More file actions
77 lines (61 loc) · 2.78 KB
/
Copy pathlazyutil.py
File metadata and controls
77 lines (61 loc) · 2.78 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
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""Utilities to support unpythonic.syntax.lazify.
This is a separate module for dependency reasons; this is regular code,
upon which other regular code is allowed to depend.
"""
__all__ = ["mark_lazy", "lazycall", "force1", "force"]
from .regutil import register_decorator
from .dynassign import make_dynvar
# HACK: break dependency loop llist -> fun -> lazyutil -> collections -> llist
#from .collections import mogrify
_init_done = False
jump = object() # gensym, nothing else "is" this
def _init_module(): # called by unpythonic.__init__ when otherwise done
global mogrify, jump, _init_done
from .collections import mogrify
from .tco import jump
_init_done = True
try: # MacroPy is optional for unpythonic
from macropy.quick_lambda import Lazy
except ImportError:
Lazy = type()
make_dynvar(_build_lazy_trampoline=False) # interaction with TCO
# -----------------------------------------------------------------------------
@register_decorator(priority=95)
def mark_lazy(f):
"""Internal. Helps calling lazy functions from outside a ``with lazify`` block."""
f._lazy = True
return f
def islazy(f):
"""Internal. Return whether the function f is marked as lazy.
When a function is marked as lazy, its arguments won't be forced by
``lazycall``. This is the only effect the mark has.
"""
# special-case "_let" for lazify/curry combo when let[] expressions are present
return hasattr(f, "_lazy") or (hasattr(f, "__name__") and f.__name__ == "_let")
def lazycall(f, *thunks, **kwthunks):
"""Internal. Helps calling strict functions from inside a ``with lazify`` block."""
if f is jump: # special case to avoid drastic performance hit in strict code
target, *argthunks = thunks
return jump(force1(target), *argthunks, **kwthunks)
if islazy(f):
return f(*thunks, **kwthunks)
return f(*force(thunks), **force(kwthunks))
# Because force(x) is more explicit than x() and MacroPy itself doesn't define this.
def force1(x):
"""Force a MacroPy lazy[] promise.
For a promise ``x``, the effect of ``force1(x)`` is the same as ``x()``,
except that ``force1 `` first checks that ``x`` is a promise.
If ``x`` is not a promise, it is returned as-is (à la Racket).
"""
return x() if isinstance(x, Lazy) else x
def force(x):
"""Like force1, but recurse into containers.
This recurses on any containers with the appropriate ``collections.abc``
abstract base classes (virtuals ok too). Mutable containers are updated
in-place, for immutables a new instance is created. For details, see
``unpythonic.collections.mogrify``.
"""
if not _init_done:
return x
return mogrify(force1, x) # in-place update to allow lazy functions to have writable list arguments