def map_(x, y): tmp = MalList("(") if type(x) == dict: x = x["fn"] for i in y: tmp.append(x(i)) return tmp
def vals(h): assert type(h) == MalList and h.opener == "{" tmp = MalList("(") for i in range(1, len(h), 2): tmp.append(h[i]) return tmp
def apply(x, *y): assert type(y[-1] == MalList) tmp = MalList("(") for i in y[:-1]: tmp.append(i) tmp.extend(y[-1]) if type(x) == dict: return x["fn"](*tmp) else: return x(*tmp)
def quasiquote(param): def in_list(param): res = MalList("(") for elt in reversed(param): if type(elt) == MalList and len(elt) > 1 and elt[0] == "splice-unquote": tmp = MalList("(") tmp.append("concat") tmp.append(elt[1]) tmp.append(res) res = tmp else: tmp = MalList("(") tmp.append("cons") tmp.append(quasiquote(elt)) tmp.append(res) res = tmp return res if type(param) == MalList and param.opener == "(" and len(param) > 0 and param[0] == "unquote": return param[1] elif type(param) == MalList and param.opener == "(": return in_list(param) elif (type(param) == MalList and param.opener == "{") or (type(param) == str): tmp = MalList("(") tmp.append("quote") tmp.append(param) return tmp elif type(param) == MalList and param.opener == "[": tmp = MalList("(") tmp.append("vec") tmp.append(in_list(param)) return tmp else: return param
def assoc(x, *y): assert len(y) % 2 == 0 assert type(x) == MalList and x.opener == "{" tmp = MalList("{") tmp_dict = {} for i in range(0, len(x), 2): tmp_dict[x[i]] = x[i + 1] for i in range(0, len(y), 2): tmp_dict[y[i]] = y[i + 1] for key in tmp_dict: tmp.append(key) tmp.append(tmp_dict[key]) return tmp
def in_list(param): res = MalList("(") for elt in reversed(param): if type(elt) == MalList and len(elt) > 1 and elt[0] == "splice-unquote": tmp = MalList("(") tmp.append("concat") tmp.append(elt[1]) tmp.append(res) res = tmp else: tmp = MalList("(") tmp.append("cons") tmp.append(quasiquote(elt)) tmp.append(res) res = tmp return res
def concat(*a): temp = MalList("(") for next_list in a: for next_item in next_list: temp.append(next_item) return temp
def cons(a, b): temp = MalList("(") temp.append(a) for next_item in b: temp.append(next_item) return temp
def vector(*x): tmp = MalList("[") for i in x: tmp.append(i) return tmp