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

Function standard_env

18-with-match/lispy/py3.10/lis.py:77–116  ·  view source on GitHub ↗

An environment with some Scheme standard procedures.

()

Source from the content-addressed store, hash-verified

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