How to use the pfun.effect.Effect function in pfun

To help you get started, we’ve selected a few pfun examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github suned / pfun / pfun / effect.py View on Github external
async def thunk():
                        next_ = f(either.get)
                        if asyncio.iscoroutine(next_):
                            effect = await next_
                        else:
                            effect = next_
                        return await effect.run_e(r)

                    return Call(thunk)

                trampoline = await self.run_e(r)
                return trampoline.and_then(cont)

            return Call(thunk)

        return Effect(run_e)
github suned / pfun / pfun / effect.py View on Github external
'Phew!'

    Args:
        effect: an `Effect` producing an `Either`

    Return:
        an `Effect` failing with `E1` or succeeding with `A1`
    """
    async def run_e(r) -> Trampoline[Either[E1, A1]]:
        async def thunk():
            trampoline = await effect.run_e(r)
            return trampoline.and_then(lambda either: Done(either.get))

        return Call(thunk)

    return Effect(run_e)
github suned / pfun / pfun / effect.py View on Github external
>>> resource.get().map(get_request)(None)
        b'content of foo.com'

        :return: ``Effect`` that produces the wrapped context manager
        """
        async def run_e(env: RuntimeEnv):
            if self.resource is None:
                # this is the first time this effect is called
                resource = self.resource_factory()  # type:ignore
                if asyncio.iscoroutine(resource):
                    resource = await resource
                object.__setattr__(self, 'resource', resource)
                await env.exit_stack.enter_async_context(self)
            return Done(self.resource)

        return Effect(run_e)
github suned / pfun / pfun / logging.py View on Github external
WARNING:root:hello!

        Args:
            msg: the log message
            stack_info: whether to include stack information in the \
                log message
            exc_info: whether to include exception info in the log message

        Return:
            `Effect` that calls `logging.warning` with `msg`
        """
        async def run_e(_):
            logging.warning(msg, stack_info=stack_info, exc_info=exc_info)
            return Done(Right(None))

        return Effect(run_e)
github suned / pfun / pfun / console.py View on Github external
>>> Console().input('What is your name? ').map(greeting).run(None)
            what is your name?  # input e.g "John Doe"
            'Hello John Doe!'

        Args:
            prompt: Prompt to dislay on stdout

        Return:
            `Effect` that reads from stdin
        """
        async def run_e(_):
            loop = asyncio.get_running_loop()
            result = await loop.run_in_executor(None, input, prompt)
            return Done(Right(result))

        return Effect(run_e)
github suned / pfun / pfun / files.py View on Github external
Args:
            path: the path of the file to be written
            content the content to write
        Return:
            `Effect` that that writes `content` to file at `path`
        """
        async def run_e(_) -> Trampoline[Either[OSError, None]]:
            try:
                with open(path, 'w') as f:
                    f.write(content)
                return Done(Right(None))
            except OSError as e:
                return Done(Left(e))

        return Effect(run_e)
github suned / pfun / pfun / effect.py View on Github external
async def run_e(r: RuntimeEnv[R]) -> Trampoline[Either[E, B]]:
            def cont(either):
                async def thunk() -> Trampoline[Either]:
                    result = f(either.get)
                    if asyncio.iscoroutine(result):
                        result = await result  # type: ignore
                    return Done(Right(result))

                if isinstance(either, Left):
                    return Done(either)
                return Call(thunk)

            trampoline = await self.run_e(r)  # type: ignore
            return trampoline.and_then(cont)

        return Effect(run_e)
github suned / pfun / pfun / logging.py View on Github external
DEBUG:root:hello!

        Args:
            msg: the log message
            stack_info: whether to include stack information in the \
                log message
            exc_info: whether to include exception info in the log message

        Return:
            `Effect` that calls `logging.debug` with `msg`
        """
        async def run_e(_):
            logging.debug(msg, stack_info=stack_info, exc_info=exc_info)
            return Done(Right(None))

        return Effect(run_e)
github suned / pfun / pfun / ref.py View on Github external
returns a `Right` wrapping a new state \
            or a `Left` value wrapping an error

        Return:
            an `Effect` that updates the state if `f` succeeds
        """
        async def run_e(_) -> Trampoline[Either[E, None]]:
            async with self.__lock:
                either = f(self.value)
                if isinstance(either, Left):
                    return Done(either)
                else:
                    object.__setattr__(self, 'value', either.get)
                return Done(Right(None))

        return Effect(run_e)
github suned / pfun / pfun / ref.py View on Github external
>>> ref.value
            'new state'

        Args:
            value: new state

        Return:
            `Effect` that updates the state
        """
        async def run_e(_) -> Trampoline[Either[NoReturn, None]]:
            async with self.__lock:
                # purists avert your eyes
                object.__setattr__(self, 'value', value)
            return Done(Right(None))

        return Effect(run_e)