示例#1
0
def tzip(f, a, b, descend=descend):
    """Map f over two types zip-wise"""
    from blaze.datashape import verify # TODO: remove circularity

    if not descend(a) or not descend(b):
        return a, b

    verify(a, b)
    result = zip(*starmap(f, zip(a.parameters, b.parameters)))
    params1, params2 = result or [(), ()]
    return (type_constructor(a)(*params1), type_constructor(b)(*params2))
示例#2
0
def tzip(f, a, b, descend=descend):
    """Map f over two types zip-wise"""
    from blaze.datashape import verify # TODO: remove circularity

    if not descend(a) or not descend(b):
        return a, b

    verify(a, b)
    params1, params2 = zip(*[
        f(arg1, arg2) for arg1, arg2 in zip(a.parameters, b.parameters)])
    return (type_constructor(a)(*params1), type_constructor(b)(*params2))
示例#3
0
def tmap(f, t, descend=descend):
    """
    Map f over t, calling `f` with type `t`. Reconstructs a new type with
    the results from `f`.
    """
    if descend(t):
        tcon = type_constructor(t)
        t = tcon(*[tmap(f, p) for p in t.parameters])
    return f(t)
示例#4
0
def transform(visitor, t, descend=descend):
    """
    Transform a type to recreate a new type
    """
    if isinstance(t, Mono):
        fn = getattr(visitor, type(t).__name__, None)
        if fn is not None:
            return fn(t)
        elif descend(t):
            tcon = type_constructor(t)
            return tcon(*[transform(visitor, p) for p in t.parameters])

    return t