SIGN IN SIGN UP
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
import operator
import time
from itertools import chain
import mal_types as types
from mal_types import MalException, List, Vector
import mal_readline
import reader
import printer
# Errors/Exceptions
def throw(obj): raise MalException(obj)
# String functions
def pr_str(*args):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
return printer.pr_list(args, " ", True)
def do_str(*args):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
return printer.pr_list(args, "", False)
def prn(*args):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
print(printer.pr_list(args, " ", True))
return None
def println(*args):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
print(printer.pr_list(args, " ", False))
return None
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
def core_readline(prompt):
try:
return mal_readline.readline(prompt)
except EOFError:
return None
def slurp(path):
with open(path) as f:
return f.read()
# Hash map functions
def assoc(src_hm, *key_vals):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
hm = types.Hash_Map(src_hm)
hm.update(types.asPairs(key_vals))
return hm
def dissoc(src_hm, *keys):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
hm = types.Hash_Map(src_hm)
for key in keys:
2017-09-13 12:00:34 +10:00
hm.pop(key, None)
return hm
def get(hm, key):
2017-09-13 12:05:04 +10:00
if hm is not None:
return hm.get(key)
else:
return None
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
contains_Q = types.Hash_Map.__contains__
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
keys = List
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
def vals(hm): return List(hm.values())
# Sequence functions
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
def cons(x, seq): return concat((x,), seq)
def concat(*lsts): return List(chain(*lsts))
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
nth = tuple.__getitem__
def first(lst):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
if lst:
return lst[0]
else: # lst is nil or empty
return None
def rest(lst):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
if lst:
it = iter(lst)
next(it)
return List(it)
else: # lst is nil or empty
return List()
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
empty_Q = operator.not_
def count(lst):
if types._nil_Q(lst): return 0
else: return len(lst)
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
def apply(f, *args): return f(*chain(args[:-1], args[-1]))
def mapf(f, lst): return List(map(f, lst))
def conj(lst, *args):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
if types._list_Q(lst):
return concat(reversed(args), lst)
else:
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
return Vector(chain(lst, args))
def seq(obj):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
if not obj:
return None # obj is nil, (), [] or ""
if types._list_Q(obj):
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
return obj
elif types._vector_Q(obj) or types._string_Q(obj):
return List(obj)
else: throw ("seq: called on non-sequence")
# Metadata functions
def with_meta(obj, meta):
new_obj = types._clone(obj)
new_obj.__meta__ = meta
return new_obj
def meta(obj):
return getattr(obj, "__meta__", None)
# Atoms functions
def deref(atm): return atm.val
def reset_BANG(atm,val):
atm.val = val
return atm.val
def swap_BANG(atm,f,*args):
atm.val = f(atm.val,*args)
return atm.val
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
ns = {
'=': types._equal_Q,
'throw': throw,
'nil?': types._nil_Q,
'true?': types._true_Q,
'false?': types._false_Q,
'number?': types._number_Q,
'string?': types._string_Q,
'symbol': types._symbol,
'symbol?': types._symbol_Q,
'keyword': types._keyword,
'keyword?': types._keyword_Q,
'fn?': lambda x: (types._function_Q(x) and not hasattr(x, '_ismacro_')),
'macro?': lambda x: (types._function_Q(x) and
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
hasattr(x, '_ismacro_')),
'pr-str': pr_str,
'str': do_str,
'prn': prn,
'println': println,
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
'readline': core_readline,
'read-string': reader.read_str,
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
'slurp': slurp,
'<': operator.lt,
'<=': operator.le,
'>': operator.gt,
'>=': operator.ge,
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.floordiv,
'time-ms': lambda : int(time.time() * 1000),
'list': types._list,
'list?': types._list_Q,
'vector': types._vector,
'vector?': types._vector_Q,
'hash-map': types._hash_map,
'map?': types._hash_map_Q,
'assoc': assoc,
'dissoc': dissoc,
'get': get,
'contains?': contains_Q,
'keys': keys,
'vals': vals,
'sequential?': types._sequential_Q,
'cons': cons,
'concat': concat,
'vec': Vector,
'nth': nth,
'first': first,
'rest': rest,
'empty?': empty_Q,
'count': count,
'apply': apply,
'map': mapf,
'conj': conj,
'seq': seq,
'with-meta': with_meta,
'meta': meta,
'atom': types._atom,
'atom?': types._atom_Q,
'deref': deref,
'reset!': reset_BANG,
python: various simplifications Use raw or multiline string litterals to make some hard-coded strings more readable. Replace some lambda constructs with more idiomatic generator expressions, removing several temporary sequences (conversions, contatenations, slices, arguments). Replace definitions with assignments when possible. Replace most python2/3 wrappers with code compatible with both versions. Use a non-printable ASCII characters instead of Unicode characters (in order to avoid unicode in python2). core: Close file open by 'slurp' after reading. coll_Q: remove as obsolete. conj: do not preserve metadata. env: Replace find with an optional argument to get. Search in outer environments with a loop instead of a recursion. (python is not intended for large recursions). readline: Copy the example from the docs. Write the history once. mal_types: clone: copy.copy() everything except callables. functions: construct fn* functions in steps, where they are used (there was no encapsulation anyway) Derive List and Vector from tuple, which is more efficient than list for immutable structures. Remove the need for __getitem__ stuff. maps: provide an asPairs iterator. printer: Introduce a pr_list helper like many other implementations. steps: Remove obviously backported stuff from first steps. Port forward the limitation of the exception backtrace size from step5 to later steps, so that REGRESS=1 does not crash stepA tests. Fix the detection of special forms (strings were accepted as well as symbols). Let try* benefit from TCO.
2024-08-08 16:08:30 +02:00
'swap!': swap_BANG,
}