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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
from pyzeta.typeclasses.monoid import Monoid from pyzeta.registry import register class StringMonoid(Monoid): @staticmethod def mempty(): return "" register('Monoid', str, StringMonoid)
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)
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)
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)
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)
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)
from pyzeta.typeclasses.monoid import Monoid from pyzeta.registry import register class ListMonoid(Monoid): @staticmethod def mempty(): return [] register('Monoid', list, ListMonoid)