Esempio n. 1
0
from toolz import compose

from pyzeta.typeclasses.functor import Functor
from pyzeta.datatypes.maybe import Maybe, Just, Nothing, fold
from pyzeta.registry import register


class MaybeFunctor(Functor):
    @staticmethod
    def fmap(f, fa):
        return fold(fa, Nothing, compose(Just, f))


register('Functor', Maybe, MaybeFunctor)
Esempio n. 2
0
from pyzeta.datatypes.maybe import Maybe, Just, Nothing, fold
from pyzeta.typeclasses.monad import Monad
from pyzeta.registry import register


class MaybeMonad(Monad):
    @staticmethod
    def bind(ma, f):
        return fold(ma, Nothing, f)


register('Monad', Maybe, MaybeMonad)
Esempio n. 3
0
from pyzeta.typeclasses.semigroup import Semigroup
from pyzeta.registry import register


class StringSemigroup(Semigroup):
    @staticmethod
    def mappend(x, y):
        return x + y


register('Semigroup', str, StringSemigroup)
Esempio n. 4
0
from pyzeta.typeclasses.functor import Functor
from pyzeta.registry import register

class ListFunctor(Functor):

    @staticmethod
    def fmap(f, fa):
        return [f(a) for a in fa]

register('Functor', list, ListFunctor)
Esempio n. 5
0
from pyzeta.typeclasses.semigroup import Semigroup
from pyzeta.registry import register
from pyzeta.datatypes.either import Either

class EitherSemigroup(Semigroup):

    @staticmethod
    def mappend(a, b):
        if a.left is not None:
            return b
        else:
            return a

register('Semigroup', Either, EitherSemigroup)
Esempio n. 6
0
from pyzeta.instances.eitherfunctor import EitherFunctor
from pyzeta.registry import register
from pyzeta.datatypes.either import Either, Right


class EitherApplicative(EitherFunctor):
    @classmethod
    def app(cls, fab, fa):
        if fab.left is not None:
            return fab
        else:
            return cls.fmap(fab.right, fa)

    @staticmethod
    def pure(a):
        return Right(a)


register('Applicative', Either, EitherApplicative)
Esempio n. 7
0
from pyzeta.typeclasses.semigroup import Semigroup
from pyzeta.registry import register


class ListSemigroup(Semigroup):
    @staticmethod
    def mappend(x, y):
        return x + y


register('Semigroup', list, ListSemigroup)
Esempio n. 8
0
from pyzeta.datatypes.maybe import Maybe, Just, Nothing, fold
from pyzeta.typeclasses.semigroup import Semigroup, mappend

from pyzeta.registry import register


class MaybeSemigroup(Semigroup):
    @staticmethod
    def mappend(x, y):
        if x is Nothing: return y
        if y is Nothing: return x
        return Just(x.v | mappend | y.v)
        # is_nothing = y
        # is_just = lambda v: Just (x |mappend| y)
        # return fold(x, is_nothing, is_just)


register('Semigroup', Maybe, MaybeSemigroup)
Esempio n. 9
0
from pyzeta.datatypes.maybe import Maybe, Just, Nothing, fold
from pyzeta.typeclasses.applicative import Applicative
from pyzeta.typeclasses.functor import fmap
from pyzeta.instances.maybefunctor import MaybeFunctor
from pyzeta.registry import register


class MaybeApplicative(Applicative):
    @staticmethod
    def app(fab, ma):
        #fa = lambda f: MaybeFunctor.fmap(f, ma)
        fa = lambda f: f | fmap | ma
        return fold(fab, Nothing, fa)

    @staticmethod
    def pure(a):
        return Maybe(a)


register('Applicative', Maybe, MaybeApplicative)
Esempio n. 10
0
from pyzeta.typeclasses.functor import Functor
from pyzeta.registry import register
from pyzeta.datatypes.either import Either, Right

class EitherFunctor(Functor):

    @staticmethod
    def fmap(f, fa):
        return Right(f(fa.right)) if fa.right else fa

register('Functor', Either, EitherFunctor)
Esempio n. 11
0
from pyzeta.typeclasses.monoid import Monoid
from pyzeta.registry import register


class StringMonoid(Monoid):
    @staticmethod
    def mempty():
        return ""


register('Monoid', str, StringMonoid)
Esempio n. 12
0
from pyzeta.datatypes.maybe import Maybe, Just, Nothing, fold
from pyzeta.typeclasses.monoid import Monoid
from pyzeta.registry import register


class MaybeMonoid(Monoid):
    @staticmethod
    def mempty():
        return Nothing


register('Monoid', Maybe, MaybeMonoid)
Esempio n. 13
0
from pyzeta.typeclasses.applicative import Applicative
from pyzeta.typeclasses.functor import fmap
from pyzeta.registry import register


class ListApplicative(Applicative):
    @staticmethod
    def app(fab, fa):
        return [f(x) for x in fa for f in fab]

    @staticmethod
    def pure(a):
        return [a]


register('Applicative', list, ListApplicative)
Esempio n. 14
0
from pyzeta.typeclasses.monad import Monad
from pyzeta.registry import register


class ListMonad(Monad):
    @staticmethod
    def bind(ma, f):
        result = []
        mmb = [f(a) for a in ma]
        for mb in mmb:
            result.extend(mb)
        return result

    @staticmethod
    def pure(a):
        return [a]


register('Monad', list, ListMonad)
Esempio n. 15
0
from pyzeta.datatypes.either import Either, Right
from pyzeta.typeclasses.monad import Monad
from pyzeta.registry import register


class EitherMonad(Monad):
    @staticmethod
    def bind(ma, f):
        if ma.left is not None:
            return ma
        else:
            return f(ma.right)


register('Monad', Either, EitherMonad)
Esempio n. 16
0
from numbers import Number

from pyzeta.typeclasses.semigroup import Semigroup
from pyzeta.registry import register

class MaxSemigroup(Semigroup):

    @staticmethod
    def mappend(x, y):
        return max(x, y)


register('Semigroup', Number, MaxSemigroup)
Esempio n. 17
0
from pyzeta.typeclasses.monoid import Monoid
from pyzeta.registry import register


class ListMonoid(Monoid):
    @staticmethod
    def mempty():
        return []


register('Monoid', list, ListMonoid)