def make_chunk(x): if hasattr(x, 'match_and_replace'): # ideally: if isinstance(x, Chunk): return x elif is_listlike(x): return Chunk(old=x, new=[[x]]) else: return Chunk(old=[x], new=[[x]])
def make_substitution(elem, env): # Encloses its answer in a list, no matter what. This enables a .subst() # method to transform a single item into multiple items. if hasattr(elem, 'subst'): return elem.subst(env) elif is_listlike(elem): return [flatten(make_substitution(e, env) for e in elem)] else: return [elem]