コード例 #1
0
def regexist(context: dict, name: str, optcheck=None) -> dict:
    if optcheck is None:
        context[name + "?"] = pythoniclam(
            lambda x: boolean(type(x) == dict and x["type"] == name),
            name + "?")
    else:
        context[name + "?"] = pythoniclam(
            lambda x: boolean(tryexplode(optcheck, x)), name + "?")
    return context
コード例 #2
0
def equal(a, b):
    if type(a) == str and type(b) == str:
        return boolean(a == b)
    elif type(a) == str and type(b) == dict:
        return equal(trycast(a, b["type"]), b)
    elif type(a) == dict and type(b) == str:
        return equal(b, a)
    elif type(a) == dict and type(b) == dict:
        if a["type"] != b["type"] or sorted(list(a.keys())) != sorted(
                list(b.keys())):
            return boolean(False)
        for key in list(a.keys()):
            v = equal(a[key], b[key])
            if not v["value"]:
                return boolean(False)
        return boolean(True)
    elif type(a) == list and type(b) == list:
        if len(a) != len(b):
            return boolean(False)
        else:
            for eix in range(0, len(a)):
                v = equal(a[eix], b[eix])
                if not v["value"]:
                    return boolean(False)
            return boolean(True)
    else:
        return boolean(a == b)
コード例 #3
0
ファイル: casting.py プロジェクト: ashdawngary/StarSLProfiler
def tryboolean(maybebool) -> Dict:
    if type(maybebool) == dict:
        if maybebool["type"] == "boolean":
            return maybebool
        else:
            raise Exception("tried to cast %s to boolean" % maybebool)
    elif type(maybebool) == str:
        if maybebool in ["#t", "#true"]:
            return boolean(True)
        elif maybebool in ["#f", "#false"]:
            return boolean(False)
        else:
            raise Exception("tried to cast %s to boolean" % maybebool)
    else:
        raise Exception("got nonsense in tryboolean eval: %s " % maybebool)
コード例 #4
0
ファイル: strops.py プロジェクト: ashdawngary/StarSLProfiler
    if len(params) == 2:
        return string(ostr[start:])
    elif len(params) == 3:
        end = trynum(params[2])["value"]
        return string(ostr[start: end])


def appendstrings(params):
    return string(foldr(list(valuesof(map(trystring, params))), lambda x, y: x + y, ""))


def areprefixes(a, b):
    if len(b) <= len(a):
        return b == a[:len(b)]


lenstr = pythoniclam(lambda x: num(len(trystring(x)["value"])), "string-length")
appstr = export_to_lam(appendstrings, "string-append")
streq = pythoniclam(lambda x, y: boolean(trystring(x)["value"] == trystring(y)["value"]), "string=?")
strle = pythoniclam(lambda x, y: boolean(trystring(x)["value"] < trystring(y)["value"]), "string<?")
strleq = pythoniclam(lambda x, y: boolean(trystring(x)["value"] <= trystring(y)["value"]), "string<=?")
strge = pythoniclam(lambda x, y: boolean(trystring(x)["value"] > trystring(y)["value"]), "string>?")
strgeq = pythoniclam(lambda x, y: boolean(trystring(x)["value"] >= trystring(y)["value"]), "string>=?")
strlo = pythoniclam(lambda x: string(trystring(x)["value"].lower()), "string-downcase")
strup = pythoniclam(lambda x: string(trystring(x)["value"].upper()), "string-upcase")
strcont = pythoniclam(lambda s, n: boolean(trystring(n)["value"] in trystring(s)["value"]), "string-contains?")
strpref = pythoniclam(lambda s, p: boolean(areprefixes(trystring(s)["value"], trystring(p)["value"])), "string-prefix?")
strsuff = pythoniclam(lambda sr, su: boolean(areprefixes(trystring(sr)["value"][::-1], trystring(su)["value"][::-1])), "string-suffix?")
strne = pythoniclam(lambda x: boolean(len(trystring(x)["value"]) > 0), "non-empty-string?")
substr = export_to_lam(substringrack, "substring")
コード例 #5
0
def getListLen(lox):
    if equal(lox, list_empty)["value"]:
        return num(0)
    elif isCons(lox):
        return num(1 + getListLen(lox["const"][1])["value"])
    else:
        raise Exception("did not take the length of a list: %s" % pttyobj(lox))


def list_racket_append(lists_to_append):
    pylists = map(topylist, lists_to_append)
    return quicklist(foldr(list(pylists), lambda x, y: list(x + y), []))


sym_eq = pythoniclam(
    lambda symx, symy: boolean(
        trysymbol(symx)["value"] == trysymbol(symy)["value"]), "symbol=?")
sym_tostr = pythoniclam(lambda symx: string(trysymbol(symx)["value"][1:]),
                        "symbol->string")
list_empty = symbol("'()")
list_fst = pythoniclam(lambda x: ensure(x, "cons")["const"][0], "first")
list_rst = pythoniclam(lambda x: ensure(x, "cons")["const"][1], "rest")
list_snd = compose(list_fst, list_rst, "second")
list_trd = compose(list_fst, compose(list_rst, list_rst), "third")
list_len = pythoniclam(lambda x: getListLen(x), "length")
list_cons = pythoniclam(cons, "cons")
list_isempty = pythoniclam(lambda x: equal(x, list_empty), "empty?")
list_iscons = pythoniclam(lambda x: boolean(isCons(x)), "cons?")
list_rev = pythoniclam(lambda x: quicklist(topylist(x)[::-1]), "reverse")
list_range = pythoniclam(
    lambda s, e, st: quicklist(
        list(
コード例 #6
0
from casting import tryboolean
from lamstructs import pythoniclam
from shortcuts import boolean, string

eq_bool = pythoniclam(
    lambda x, y: boolean(tryboolean(x)["value"] == tryboolean(y)["value"]),
    "boolean=?")
not_bool = pythoniclam(lambda x: boolean(not tryboolean(x)["value"]), "not")
bool_tostr = pythoniclam(lambda x: string(str(tryboolean(x)["value"])),
                         "boolean->string")
コード例 #7
0
def num_min(nums: List):
    q = list(map(trynum, nums))
    return {"type": "number", "value": min(valuesof(q))}


add_num = export_to_lam(num_add, "+")
mul_num = export_to_lam(num_mul, "*")
sub_num = export_to_lam(num_sub, "-")
div_num = export_to_lam(num_div, "/")
max_num = export_to_lam(num_max, "max")
min_num = export_to_lam(num_min, "min")
add1_num = pythoniclam(lambda x: num(trynum(x)["value"] + 1), "add1")
sub1_num = pythoniclam(lambda x: num(trynum(x)["value"] - 1), "sub1")
abs_num = pythoniclam(lambda x: num(abs(trynum(x)["value"])), "abs")
eq_num = pythoniclam(
    lambda x, y: boolean(trynum(x)["value"] == trynum(y)["value"]), "=")
ge_num = pythoniclam(
    lambda x, y: boolean(trynum(x)["value"] > trynum(y)["value"]), ">")
geq_num = pythoniclam(
    lambda x, y: boolean(trynum(x)["value"] >= trynum(y)["value"]), ">=")
le_num = pythoniclam(
    lambda x, y: boolean(trynum(x)["value"] < trynum(y)["value"]), "<")
leq_num = pythoniclam(
    lambda x, y: boolean(trynum(x)["value"] <= trynum(y)["value"]), "<=")
modu_num = pythoniclam(
    lambda x, y: num(trynum(x)["value"] % trynum(y)["value"]), "modulo")
sqr_num = pythoniclam(lambda x: num(trynum(x)["value"]**2), "sqr")
sqrt_num = pythoniclam(lambda x: num(sqrt(trynum(x)["value"])), "sqrt")
num_tostr = pythoniclam(lambda x: string(consisestr(trynum(x)["value"])),
                        "number->string")
num_even = pythoniclam(lambda x: boolean(trynum(x)["value"] % 2 == 0), "even?")