How to use the pfun.list.List 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 / sql.py View on Github external
... )
        >>> class User(pfun.Immutable):
        ...     name: str
        ...     age: int
        >>> results.and_then(as_type(User))(None)
        List((User(name='bob', age=32),))

    Args:
        type_: type to convert database results to
        results: database results to convert

    :return: ``List`` of database results converted to `type_`
    """

    try:
        return success(List(type_(**row) for row in results)  # type: ignore
                       )
    except TypeError as e:
        return error(e)
github suned / pfun / pfun / list.py View on Github external
def reverse(self) -> 'List[A]':
        return List(reversed(self))
github suned / pfun / pfun / sql.py View on Github external
async def _fetch(connection: asyncpg.Connection
                         ) -> Try[asyncpg.PostgresError, Results]:
            try:
                result = await connection.fetch(query, *args, timeout=timeout)
                result = List(Dict(record) for record in result)
                return success(result)
            except asyncpg.PostgresError as e:
                return error(e)
github suned / pfun / pfun / list.py View on Github external
def empty(self) -> 'List[A]':
        return List()
github suned / pfun / pfun / list.py View on Github external
def filter(self, f: Callable[[A], bool]) -> 'List[A]':
        """
        Filter elements by the predicate ``f``

        Example:
            >>> List(range(4)).filter(lambda e: e % 2 == 0)
            [0, 2]

        Args:
            f: Function to filter by
        Return:
            new `List` filtered by ``f``
        """
        return List(filter(f, self))
github suned / pfun / pfun / sql.py View on Github external
class PostgresConnection(Immutable):
    """
    Wrapper for `asyncpg.Connection` to make it useable with
    `Resource`
    """
    connection: asyncpg.Connection

    async def __aenter__(self):
        pass

    async def __aexit__(self, *args, **kwargs):
        await self.connection.close()


Results = List[Dict[str, Any]]
"""
Type-alias for `pfun.list.List[pfun.dict.Dict[str, typing.Any]]`
"""
Results.__module__ = __name__

T = TypeVar('T')


@curry
def as_type(type_: Type[T], results: Results) -> Try[TypeError, List[T]]:
    """
    Convert database results to `type_`

    Example:
        >>> results = pfun.effect.success(
        ...     pfun.List([pfun.Dict(dict(name='bob', age=32))])
github suned / pfun / pfun / list.py View on Github external
def map(self, f: Callable[[A], B]) -> 'List[B]':
        """
        Apply ``f`` to each element in the list

        Example:
            >>> List(range(2)).map(str)
            ['0', '1']

        Args:
            f: Function to apply
        Return:
            new `List` mapped by ``f``
        """
        return List(map(f, self))
github suned / pfun / pfun / maybe.py View on Github external
"""
    Extract value from each `Maybe`, ignoring
    elements that are `Nothing`


    Example:
        >>> flatten([Just(1), Nothing(), Just(2)])
        List((1, 2))

    Args:
        maybes: Seqence of `Maybe`
    Return:
        `List` of unwrapped values
    """
    justs = [m for m in maybes if isinstance(m, Just)]
    return List(j.get for j in justs)
github suned / pfun / pfun / list.py View on Github external
def and_then(self, f: 'Callable[[A], List[B]]') -> 'List[B]':
        """
        Chain together functions that produce more than one result

        Example:
            >>> List(range(4)).and_then(lambda v: List(range(v)))
            [0, 0, 1, 0, 1, 2]

        Args:
            f: Function to apply to elements of this `List`
        Return:
            Concatenated results from applying ``f`` to all elements
        """
        return self.reduce(lambda l, v: l + f(v), List())