コード例 #1
0
ファイル: foldable.py プロジェクト: yangyang202/DA_GP9
    def fold_m(self, fa: F[A], z: G, f: Callable[[B, A], G]) -> G:
        monad = Monad.fatal(type(z))

        def folder(z1: G, a: A) -> G:
            return monad.flat_map(z1, lambda b: f(b, a))

        return self.fold_left(fa)(z)(folder)
コード例 #2
0
 def send(val: B) -> F[B]:
     nonlocal c, m
     try:
         c = itr.send(val)
         m = Monad.fatal_for(c)
         return c.flat_map(send)
     except StopIteration:
         return m.pure(val)
コード例 #3
0
 def flat_map(self) -> None:
     a = 'start'
     b = 'end'
     Maybe.optional(a).flat_map(
         lambda v: Maybe.optional(v + b)).should.contain(a + b)
     f = lambda a: Maybe.optional(a).flat_map(lambda c: Monad.fatal(Maybe).
                                              pure(c + b))
     f(a).should.contain(a + b)
コード例 #4
0
    def traverse(self, fa: Map[Any, A], f: Callable[[A], B],
                 tpe: Type[G]) -> G:
        monad = Monad.fatal(tpe)

        def folder(z, kv: Tuple[A, B]):
            k, v = kv
            return monad.map2(z.product(f(v)), lambda l, b: l.cat((k, b)))

        return fa.to_list.fold_left(monad.pure(Map()))(folder)
コード例 #5
0
ファイル: do.py プロジェクト: GitHub-Notables/amino
 def do_loop(*a: Any, **kw: Any) -> F[B]:
     itr = f(*a, **kw)
     if not isinstance(itr, GeneratorType):
         raise Exception(f'function `{f.__qualname__}` decorated with `do` does not produce a generator')
     init = itr.send(None)
     m = Monad.fatal_for(init)
     @functools.wraps(f)
     def loop(val: B) -> F[B]:
         try:
             return m.flat_map(itr.send(val), loop)
         except StopIteration as e:
             return m.pure(val if e.value is None else e.value)
     return m.flat_map(init, loop)
コード例 #6
0
ファイル: state.py プロジェクト: tangleibest/untitled
 def __new__(cls,
             name: str,
             bases: tuple,
             ns: dict,
             tpe: Type[G] = None,
             **kw: Any) -> Type['StateTMeta']:
     cls.monad: Monad = ...
     if tpe is not None:
         inst = super().__new__(cls,
                                name,
                                bases + (F, ),
                                ns,
                                implicits=True,
                                auto=True,
                                **kw)
         inst.tpe = tpe  # type: ignore
         inst.monad = cast(Monad, Monad.fatal(tpe))  # type: ignore
         return inst
     else:
         return super().__new__(cls, name, bases, ns, tpe, **kw)
コード例 #7
0
ファイル: either.py プロジェクト: yangyang202/DA_GP9
from amino.tc.monad import Monad
from amino.tc.zip import Zip
from amino.instances.list import ListTraverse
from amino import List, curried
from amino.util.string import ToStr
from amino.state.base import StateT
from amino.either import Either

A = TypeVar('A')
B = TypeVar('B')
S = TypeVar('S')
R = TypeVar('R')
ST1 = TypeVar('ST1')
E = TypeVar('E')

monad: Monad = cast(Monad, Monad.fatal(Either))


class EitherStateCtor(Generic[S]):
    def inspect(self, f: Callable[[S], A]) -> 'EitherState[E, S, A]':
        def g(s: S) -> Either[E, Tuple[S, A]]:
            return monad.pure((s, f(s)))

        return EitherState.apply(g)

    def inspect_f(self, f: Callable[[S], Either[E,
                                                A]]) -> 'EitherState[E, S, A]':
        def g(s: S) -> Either[E, Tuple[S, A]]:
            return f(s).map(lambda a: (s, a))

        return EitherState.apply(g)
コード例 #8
0
from amino.tc.base import ImplicitsMeta, Implicits
from amino.tc.monad import Monad
from amino.tc.zip import Zip
from amino.instances.list import ListTraverse
from amino import List, curried
from amino.util.string import ToStr
from amino.state.base import StateT
from amino.id import Id

A = TypeVar('A')
B = TypeVar('B')
S = TypeVar('S')
R = TypeVar('R')
ST1 = TypeVar('ST1')

monad: Monad = cast(Monad, Monad.fatal(Id))


class IdStateCtor(Generic[S]):
    def inspect(self, f: Callable[[S], A]) -> 'IdState[S, A]':
        def g(s: S) -> Id[Tuple[S, A]]:
            return monad.pure((s, f(s)))

        return IdState.apply(g)

    def inspect_f(self, f: Callable[[S], Id[A]]) -> 'IdState[S, A]':
        def g(s: S) -> Id[Tuple[S, A]]:
            return f(s).map(lambda a: (s, a))

        return IdState.apply(g)
コード例 #9
0
from amino.tc.base import ImplicitsMeta, Implicits
from amino.tc.monad import Monad
from amino.tc.zip import Zip
from amino.instances.list import ListTraverse
from amino import List, curried
from amino.util.string import ToStr
from amino.state.base import StateT
from amino.maybe import Maybe

A = TypeVar('A')
B = TypeVar('B')
S = TypeVar('S')
R = TypeVar('R')
ST1 = TypeVar('ST1')

monad: Monad = cast(Monad, Monad.fatal(Maybe))


class MaybeStateCtor(Generic[S]):
    def inspect(self, f: Callable[[S], A]) -> 'MaybeState[S, A]':
        def g(s: S) -> Maybe[Tuple[S, A]]:
            return monad.pure((s, f(s)))

        return MaybeState.apply(g)

    def inspect_f(self, f: Callable[[S], Maybe[A]]) -> 'MaybeState[S, A]':
        def g(s: S) -> Maybe[Tuple[S, A]]:
            return f(s).map(lambda a: (s, a))

        return MaybeState.apply(g)
コード例 #10
0
ファイル: either.py プロジェクト: yangyang202/DA_GP9
 def acc(v: A) -> 'Either[A, C]':
     monoid = Monoid.fatal_for(self.__left_value)
     monad = Monad.fatal_for(self.__left_value)
     return monoid.combine(self.__left_value, monad.pure(v))
コード例 #11
0
from amino.tc.base import ImplicitsMeta, Implicits
from amino.tc.monad import Monad
from amino.tc.zip import Zip
from amino.instances.list import ListTraverse
from amino import List, curried
from amino.util.string import ToStr
from amino.state.base import StateT
from amino.eval import Eval

A = TypeVar('A')
B = TypeVar('B')
S = TypeVar('S')
R = TypeVar('R')
ST1 = TypeVar('ST1')

monad: Monad = cast(Monad, Monad.fatal(Eval))


class EvalStateCtor(Generic[S]):
    def inspect(self, f: Callable[[S], A]) -> 'EvalState[S, A]':
        def g(s: S) -> Eval[Tuple[S, A]]:
            return monad.pure((s, f(s)))

        return EvalState.apply(g)

    def inspect_f(self, f: Callable[[S], Eval[A]]) -> 'EvalState[S, A]':
        def g(s: S) -> Eval[Tuple[S, A]]:
            return f(s).map(lambda a: (s, a))

        return EvalState.apply(g)
コード例 #12
0
ファイル: state.py プロジェクト: tek/chiasma-py
import abc
from amino.tc.base import TypeClass, tc_prop
from amino.state import State, EitherState
from chiasma.tmux import Tmux
from amino import IO, Maybe, Either
from amino.func import CallByName
E = TypeVar('E')

A = TypeVar('A')
B = TypeVar('B')
S = TypeVar('S')
R = TypeVar('R')
ST1 = TypeVar('ST1')

monad: Monad = cast(Monad, Monad.fatal(TmuxIO))


class TmuxIOStateCtor(Generic[S]):
    def inspect(self, f: Callable[[S], A]) -> 'TmuxIOState[S, A]':
        def g(s: S) -> TmuxIO[Tuple[S, A]]:
            return monad.pure((s, f(s)))

        return TmuxIOState.apply(g)

    def inspect_f(self, f: Callable[[S], TmuxIO[A]]) -> 'TmuxIOState[S, A]':
        def g(s: S) -> TmuxIO[Tuple[S, A]]:
            return f(s).map(lambda a: (s, a))

        return TmuxIOState.apply(g)