catthy
Advanced tools
| from __future__ import annotations | ||
| from typing import * | ||
| from abc import ABC, abstractmethod, abstractstaticmethod | ||
| A = TypeVar('A') | ||
| B = TypeVar('B') | ||
| K = TypeVar('K') | ||
| V = TypeVar('V') | ||
| K_1 = TypeVar('K_1') | ||
| V_1 = TypeVar('V_1') | ||
| Rule = Callable[[A], bool] | ||
| MapFunction = Callable[[A], B] | ||
| BinaryOperation = Callable[[A, A], B] | ||
| DictMapFunction = Callable[[K,V], Tuple[K_1, V_1]] | ||
| class SupportsIter(Protocol[A]): | ||
| def __iter__(self) -> A: ... | ||
| class SupportsNext(Protocol[A]): | ||
| def __next__(self) -> A: ... | ||
| class SupportsIterNext(SupportsIter, SupportsNext): ... | ||
| class SupportsMap(Protocol[A]): | ||
| @abstractmethod | ||
| def map(self, f: MapFunction[A, B]) -> SupportsMap[B]: ... | ||
| class SupportsZip(Protocol[A]): | ||
| @abstractmethod | ||
| def zip(self, *Is: Iterable) -> SupportsZip: ... | ||
| class Foldable(Protocol[A]): | ||
| @abstractmethod | ||
| def foldr(self, binop: BinaryOperation[A, B]) -> B: ... | ||
| @abstractmethod | ||
| def foldl(self, binop: BinaryOperation[A, B]) -> B: ... | ||
| class SupportsFilter(Protocol[A]): | ||
| @abstractmethod | ||
| def filter(self, f: Rule[A]) -> SupportsFilter: ... | ||
| class SupportsZipMap(SupportsZip[A], SupportsMap[A]): | ||
| @abstractmethod | ||
| def zip_map(self, f: MapFunction[A, B]) -> SupportsZipMap: ... | ||
| @abstractmethod | ||
| def zip_maps(self, *fs: Callable) -> SupportsZipMap: ... | ||
| class Functor(SupportsMap[A]): | ||
| @abstractmethod | ||
| def map(self, f: MapFunction[A, B]) -> Functor[B]: ... | ||
| class Applicative(Functor[A]): | ||
| @abstractstaticmethod | ||
| def of(cls, *xs) -> Applicative[A]: ... | ||
| @abstractmethod | ||
| def apply_to(self: Applicative[MapFunction[A, B]], other: Applicative[A]) -> Applicative[B]: ... |
| from __future__ import annotations | ||
| from .functor import DefaultFunctor | ||
| from typing import Callable, Iterable, TypeVar, Tuple | ||
| from .definitions import SupportsFilter, SupportsZip, Foldable | ||
| from .folds import foldl, foldr, _initial_missing, _default_initial | ||
| A = TypeVar('A') | ||
| B = TypeVar('B') | ||
| class FunctionCastable: | ||
| def cast_after(self, f: Callable) -> FunctionCastable: | ||
| self = type(self)(f(self)) | ||
| return self | ||
| @classmethod | ||
| def cast(cls, x) -> FunctionCastable: | ||
| return cls(x) | ||
| class DefaultFiltrable(SupportsFilter[A]): | ||
| def filter(self, f: Callable) -> DefaultFiltrable: | ||
| return type(self)(filter(f, self)) | ||
| class DefaultZippable(SupportsZip[A]): | ||
| def zip(self, *others: Iterable) -> DefaultZippable[Tuple]: | ||
| return type(self)(zip(self, *others)) | ||
| class DefaultMappable(DefaultFunctor[A]): ... | ||
| class DefaultZipMappable(DefaultZippable[A], DefaultMappable[A]): | ||
| def zip_map(self, f, other = None) -> DefaultZipMappable[Tuple]: | ||
| if other is not None: | ||
| return self.zip(other.map(f)) | ||
| else: | ||
| return self.zip(self.map(f)) | ||
| def zip_maps(self, *fs, keep=False): | ||
| if keep: | ||
| return type(self)( self.zip(*(self.map(f) for f in fs)) ) | ||
| else: | ||
| return type(self)( zip(*(self.map(f) for f in fs)) ) | ||
| class DefaultFoldable(Foldable[A]): | ||
| def foldr(self, binop: Callable, initial=_default_initial): | ||
| return foldr(binop, self, initial) | ||
| def foldl(self, binop: Callable, initial=_default_initial): | ||
| return foldl(binop, self, initial) | ||
| class ExtraFoldable(DefaultFoldable[A]): | ||
| def sum(self) -> A: | ||
| return sum(self) | ||
| def prod(self) -> A: | ||
| return self.foldl(lambda a,b: a*b) | ||
| def max(self) -> A: | ||
| return max(self) | ||
| def min(self) -> A: | ||
| return min(self) | ||
| class ExtraFiltrable(DefaultFiltrable[A]): | ||
| def split_by(self, f: Callable[[A], bool]) -> Tuple[DefaultFiltrable[A]]: | ||
| return (self.filter(f), self.filter(lambda x: not f(x))) | ||
| class ExtraFunctor(DefaultFunctor[A]): | ||
| def split_map(self, f: Callable[[A], B]) -> Tuple[ExtraFunctor[A], ExtraFunctor[B]]: | ||
| return (self, self.map(f)) | ||
| def split_maps(self, *fs: Callable) -> Tuple[ExtraFunctor]: | ||
| return (self, *(self.map(f) for f in fs)) |
| from typing import TypeVar, Callable, Iterable | ||
| A = TypeVar('A') | ||
| B = TypeVar('B') | ||
| # Adapted from functools.reduce | ||
| class _initial_missing(object): ... | ||
| _default_initial = _initial_missing() | ||
| def foldl(function, sequence, initial=_default_initial): | ||
| it = iter(sequence) | ||
| if isinstance(initial, _initial_missing): | ||
| try: | ||
| value = next(it) | ||
| except StopIteration: | ||
| raise TypeError( | ||
| "foldl() of empty iterable with no initial value") from None | ||
| else: | ||
| value = initial | ||
| for element in it: | ||
| value = function(value, element) | ||
| return value | ||
| def foldr(binary_operation: Callable[[A,B], B], sequence: Iterable, initial=_default_initial) -> B: | ||
| it = iter(sequence) | ||
| if isinstance(initial, _initial_missing): | ||
| try: | ||
| value = next(it) | ||
| except StopIteration: | ||
| raise TypeError( | ||
| "foldr() of empty iterable with no initial value") from None | ||
| else: | ||
| value = initial | ||
| for element in it: | ||
| value = binary_operation(element, value) | ||
| return value |
| from __future__ import annotations | ||
| from typing import Callable, Protocol, TypeVar | ||
| A = TypeVar('A') | ||
| class FactoryConstructor(Protocol[A]): | ||
| @classmethod | ||
| def of(cls, *xs) -> FactoryConstructor[A]: | ||
| return cls(xs) | ||
| class NaiveHashable: | ||
| def __hash__(self): | ||
| return hash(str(self)) | ||
| class FunctionCastable: | ||
| def cast_after(self, f: Callable) -> FunctionCastable: | ||
| self = type(self)(f(self)) | ||
| return self | ||
| @classmethod | ||
| def cast(cls, x) -> FunctionCastable: | ||
| return cls(x) | ||
| def translate(mappings: dict): | ||
| def add_mapping(cls): | ||
| for k,v in mappings.items(): | ||
| setattr(cls, v, getattr(cls, k)) | ||
| return cls | ||
| return add_mapping |
| Metadata-Version: 2.1 | ||
| Name: catthy | ||
| Version: 0.0.2 | ||
| Version: 0.0.3 | ||
| Summary: Functional Python library. | ||
@@ -5,0 +5,0 @@ Home-page: https://github.com/joangq/catthy |
| README.md | ||
| setup.py | ||
| catthy/__init__.py | ||
| catthy/fun.py | ||
| catthy/definitions.py | ||
| catthy/extra.py | ||
| catthy/folds.py | ||
| catthy/util.py | ||
| catthy.egg-info/PKG-INFO | ||
@@ -6,0 +9,0 @@ catthy.egg-info/SOURCES.txt |
+27
-2
@@ -1,2 +0,27 @@ | ||
| from catthy.fun import EnhancedContainer, NaiveHashable, FunctionCastable, Functor, DefaultFunctor, DefaultFunctorDict, Applicative, DefaultApplicative | ||
| from catthy.fun import FList, FTuple, FSet, FDeque, FDict, FOrderedDict, AList, ATuple, ASet, ADeque | ||
| from .definitions import * | ||
| from .folds import * | ||
| from .functor import * | ||
| from .applicative import * | ||
| from .extra import * | ||
| from .util import * | ||
| from collections import deque | ||
| @translate({'of': 'de'}) | ||
| class tupla(DefaultApplicative, ExtraFunctor, DefaultZipMappable, ExtraFoldable, ExtraFiltrable, tuple) : ... | ||
| @translate({'of': 'de'}) | ||
| class lista(DefaultApplicative, ExtraFunctor, DefaultZipMappable, ExtraFoldable, ExtraFiltrable, list): ... | ||
| @translate({'of': 'de', | ||
| 'popleft': 'desencolar'}) | ||
| class cola(DefaultApplicative, ExtraFunctor, DefaultZipMappable, ExtraFoldable, ExtraFiltrable, deque) : ... | ||
| @translate({'of': 'de', | ||
| 'pop': 'desapilar'}) | ||
| class pila(DefaultApplicative, ExtraFunctor, DefaultZipMappable, ExtraFoldable, ExtraFiltrable, deque) : ... | ||
| @translate({'of': 'de'}) | ||
| class conjunto(DefaultApplicative, ExtraFunctor, DefaultZipMappable, ExtraFoldable, ExtraFiltrable, NaiveHashable, set): ... | ||
+1
-1
| Metadata-Version: 2.1 | ||
| Name: catthy | ||
| Version: 0.0.2 | ||
| Version: 0.0.3 | ||
| Summary: Functional Python library. | ||
@@ -5,0 +5,0 @@ Home-page: https://github.com/joangq/catthy |
+1
-1
@@ -5,3 +5,3 @@ from setuptools import setup, find_packages | ||
| "name":"catthy", | ||
| "version":"0.0.2", | ||
| "version":"0.0.3", | ||
| "description":"Functional Python library.", | ||
@@ -8,0 +8,0 @@ "long_description":"Functional Python library for playing around with functors.", |
-101
| from __future__ import annotations | ||
| from typing import Generic, TypeVar, Callable, List, Tuple, Set, Dict, NamedTuple, Deque, ChainMap, Counter, OrderedDict, DefaultDict | ||
| from collections import deque, ChainMap, Counter, OrderedDict | ||
| from abc import ABC, abstractmethod, abstractclassmethod | ||
| A = TypeVar('A') | ||
| B = TypeVar('B') | ||
| C = TypeVar('C') | ||
| K = TypeVar('K') | ||
| V = TypeVar('V') | ||
| K_1 = TypeVar('K_1') | ||
| V_1 = TypeVar('V_1') | ||
| MapFunction = Callable[[A], B] | ||
| DictMapFunction = Callable[[K,V], Tuple[K_1, V_1]] | ||
| class EnhancedContainer(Generic[A]): | ||
| @classmethod | ||
| def of(cls, *xs) -> EnhancedContainer[A]: | ||
| return cls(xs) | ||
| class NaiveHashable: | ||
| def __hash__(self): | ||
| return hash(str(self)) | ||
| class FunctionCastable: | ||
| def cast(self, f: Callable) -> FunctionCastable: | ||
| self = type(self)(f(self)) | ||
| return self | ||
| class Functor(ABC, Generic[A]): | ||
| @abstractmethod | ||
| def map(self, f: MapFunction[A, B]) -> Functor[B]: ... | ||
| class DefaultFunctor(Generic[A]): | ||
| def map(self, f: MapFunction[A, B]) -> DefaultFunctor[B]: | ||
| self = type(self)(f(x) for x in self) | ||
| return self | ||
| class DefaultFunctorDict(Generic[A]): | ||
| def map(self, f: DictMapFunction[K,V, K_1,V_1]) -> DefaultFunctorDict[K_1, V_1]: | ||
| self = type(self)({f(k,v) for k,v in self.items()}) | ||
| return self | ||
| class Applicative(Functor, ABC, Generic[A]): | ||
| @abstractclassmethod | ||
| def of(cls, *xs) -> Applicative[A]: ... # pure | ||
| class DefaultApplicative(EnhancedContainer, Functor): ... | ||
| # Concrete Functors ===================================================== # | ||
| class FList(List[A], DefaultFunctor): ... | ||
| class FTuple(Tuple[A], DefaultFunctor): ... | ||
| class FSet(Set[A], DefaultFunctor): ... | ||
| class FDeque(Deque[A], DefaultFunctor): ... | ||
| class FDict(Dict[K,V], DefaultFunctorDict): ... | ||
| class FOrderedDict(OrderedDict[K,V], DefaultFunctorDict): ... | ||
| # Concrete Applicatives ================================================= # | ||
| class AList(FList[A], DefaultApplicative): ... | ||
| class ATuple(FTuple[A], DefaultApplicative): ... | ||
| class ASet(FSet[A], DefaultApplicative): ... | ||
| class ADeque(FDeque[A], DefaultApplicative): ... | ||
| # = | ||
| from functools import _initial_missing, wraps, reduce as foldl | ||
| from typing import Any, Iterable | ||
| def reverse(binary_operation: Callable[[A,B], Any]) -> Callable[[B,A], Any]: | ||
| return wraps(binary_operation)(lambda a, b: binary_operation(b, a)) | ||
| # foldr = foldl(reverse(binary_operation), sequence, initial) | ||
| def foldr(binary_operation: Callable[[A,B], B], sequence: Iterable, initial=_initial_missing) -> B: | ||
| it = iter(sequence) | ||
| if initial is _initial_missing: | ||
| try: | ||
| value = next(it) | ||
| except StopIteration: | ||
| raise TypeError( | ||
| "foldr() of empty iterable with no initial value") from None | ||
| else: | ||
| value = initial | ||
| for element in it: | ||
| value = binary_operation(element, value) | ||
| return value |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
8593
73.25%13
30%189
112.36%