Ejemplo n.º 1
0
def all_(f, t):
    """``all :: Foldable t => (a -> bool) -> t a -> bool``

    Determines whether all elements of the structure satisfy the predicate.

    """
    from hask3.Data import List as DL
    return DL.all_(toList(t))
Ejemplo n.º 2
0
def concat(t):
    """``concat :: Foldable t => t [a] -> [a]``

    The concatenation of all the elements of a container of lists.

    """
    from hask3.Data import List as DL
    return DL.concat(toList(t))
Ejemplo n.º 3
0
def minimumBy_(f, t):
    """``minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a``

    The least element of a non-empty structure with respect to the given
    comparison function.

    """
    from hask3.Data import List as DL
    return DL.minimumBy(toList(t))
Ejemplo n.º 4
0
def concatMap(f, t):
    """``concatMap :: Foldable t => (a -> [b]) -> t a -> [b]``

    Map a function over all the elements of a container and concatenate the
    resulting lists.

    """
    from hask3.Data import List as DL
    return DL.concatMap(f, toList(t))
Ejemplo n.º 5
0
def find(f, t):
    """``find :: Foldable t => (a -> bool) -> t a -> Maybe a``

    The find function takes a predicate and a structure and returns the
    leftmost element of the structure matching the predicate, or Nothing if
    there is no such element.

    """
    from hask3.Data import List as DL
    return DL.find(f, toList(t))
Ejemplo n.º 6
0
def or_(t):
    """``or :: Foldable t => t bool -> bool``

    or returns the disjunction of a container of Bools.  For the result to be
    False, the container must be finite; True, however, results from a True
    value finitely far from the left end.

    """
    from hask3.Data import List as DL
    return DL.or_(toList(t))
Ejemplo n.º 7
0
def and_(t):
    """``and :: Foldable t => t bool -> bool``

    and returns the conjunction of a container of Bools.  For the result to be
    True, the container must be finite; False, however, results from a False
    value finitely far from the left end.

    """
    from hask3.Data import List as DL
    return DL.and_(toList(t))
Ejemplo n.º 8
0
    def make_instance(typeclass, cls, foldr, foldr1=None, foldl=None,
            foldl_=None, foldl1=None, toList=None, null=None, length=None,
            elem=None, maximum=None, minimum=None, sum=None, product=None):
        from hask3.hack import is_builtin
        from hask3.lang.type_system import build_instance
        from hask3.lang.lazylist import L
        from hask3.Data import List as DL

        # attributes that are not supplied are implemented in terms of toList
        if toList is None:
            if hasattr(cls, "__iter__"):
                toList = lambda x: L[iter(x)]
            else:
                toList = lambda t: foldr(lambda x, y: x ^ y, L[[]], t)

        # TODO: Automate this
        foldr1 = (lambda x: DL.foldr1(toList(x))) if foldr1 is None else foldr1
        foldl = (lambda x: DL.foldl(toList(x))) if foldl is None else foldl
        foldl_ = (lambda x: DL.foldl_(toList(x))) if foldl_ is None else foldl_
        foldl1 = (lambda x: DL.foldl1(toList(x))) if foldl1 is None else foldl1
        null = (lambda x: DL.null(toList(x))) if null is None else null
        length = (lambda x: DL.length(toList(x))) if length is None else length
        elem = (lambda x: DL.length(toList(x))) if length is None else length
        mi = (lambda x: DL.minimum(toList(x))) if minimum is None else minimum
        ma = (lambda x: DL.maximum(toList(x))) if maximum is None else maximum
        sum = (lambda x: DL.sum(toList(x))) if sum is None else sum
        p = (lambda x: DL.product(toList(x))) if product is None else product

        attrs = {"foldr": foldr, "foldr1": foldr1, "foldl": foldl,
                 "foldl_": foldl_, "foldl1": foldl1, "toList": toList,
                 "null": null, "length": length, "elem": elem, "maximum": ma,
                 "minimum": mi, "sum": sum, "product": p}
        build_instance(Foldable, cls, attrs)

        if not hasattr(cls, "__len__") and not is_builtin(cls):
            cls.__len__ = length

        if not hasattr(cls, "__iter__") and not is_builtin(cls):
            cls.__iter__ = lambda x: iter(toList(x))