MCPcopy
hub / github.com/fluentpython/example-code-2e / standard_env

Function standard_env

18-with-match/lispy/py3.9/lis.py:74–113  ·  view source on GitHub ↗

An environment with some Scheme standard procedures.

()

Source from the content-addressed store, hash-verified

72
73
74def standard_env() -> Environment:
75 "An environment with some Scheme standard procedures."
76 env = Environment()
77 env.update(vars(math)) # sin, cos, sqrt, pi, ...
78 env.update({
79 '+': op.add,
80 '-': op.sub,
81 '*': op.mul,
82 '/': op.truediv,
83 'quotient': op.floordiv,
84 '>': op.gt,
85 '<': op.lt,
86 '>=': op.ge,
87 '<=': op.le,
88 '=': op.eq,
89 'abs': abs,
90 'append': lambda *args: list(chain(*args)),
91 'apply': lambda proc, args: proc(*args),
92 'begin': lambda *x: x[-1],
93 'car': lambda x: x[0],
94 'cdr': lambda x: x[1:],
95 'cons': lambda x, y: [x] + y,
96 'display': lambda x: print(lispstr(x)),
97 'eq?': op.is_,
98 'equal?': op.eq,
99 'filter': lambda *args: list(filter(*args)),
100 'length': len,
101 'list': lambda *x: list(x),
102 'list?': lambda x: isinstance(x, list),
103 'map': lambda *args: list(map(*args)),
104 'max': max,
105 'min': min,
106 'not': op.not_,
107 'null?': lambda x: x == [],
108 'number?': lambda x: isinstance(x, (int, float)),
109 'procedure?': callable,
110 'round': round,
111 'symbol?': lambda x: isinstance(x, Symbol),
112 })
113 return env
114
115
116################ Interaction: A REPL

Callers 4

lis_test.pyFile · 0.90
std_envFunction · 0.90
replFunction · 0.70
runFunction · 0.70

Calls 3

EnvironmentClass · 0.70
lispstrFunction · 0.70
updateMethod · 0.45

Tested by 1

std_envFunction · 0.72