Skip to content

Commit

Permalink
adjust variance
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyNikiforov committed Oct 10, 2024
1 parent 8092d46 commit 797c584
Showing 1 changed file with 45 additions and 35 deletions.
80 changes: 45 additions & 35 deletions src/foundation/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
from typing import Callable, Tuple, TypeVar

_Tin = TypeVar("_Tin")
_Tin2 = TypeVar("_Tin2")
_Tin3 = TypeVar("_Tin3")
_Tout = TypeVar("_Tout")
_Tinter = TypeVar("_Tinter")
_T_contra = TypeVar("_T_contra", contravariant=True)
_T2_contra = TypeVar("_T2_contra", contravariant=True)
_T3_contra = TypeVar("_T3_contra", contravariant=True)
_T_co = TypeVar("_T_co", covariant=True)
_T_inv = TypeVar("_T_inv")


def compose(f: Callable[[_Tinter], _Tout], g: Callable[[_Tin], _Tinter]) -> Callable[[_Tin], _Tout]:
def compose(
f: Callable[[_T_inv], _T_co], g: Callable[[_T_contra], _T_inv]
) -> Callable[[_T_contra], _T_co]:
"""
`f after g` composition of functions
Equiv: lamdba x -> f(g(x))
"""

def inter_(value: _Tin) -> _Tout:
def inter_(value: _T_contra) -> _T_co:
return f(g(value))

return inter_


def identity(value: _Tin) -> _Tin:
def identity(value: _T_inv) -> _T_inv:
"""
identity function
"""
return value


def constant(value: _Tout) -> Callable[[_Tin], _Tout]:
def constant(value: _T_inv) -> Callable[[_T_contra], _T_inv]:
"""
constant function
"""

def _intern(_: _Tin) -> _Tout:
def _intern(_: _T_contra) -> _T_inv:
return value

return _intern


def pipe(f: Callable[[_Tin], _Tinter], g: Callable[[_Tinter], _Tout]) -> Callable[[_Tin], _Tout]:
def pipe(
f: Callable[[_T_contra], _T_inv], g: Callable[[_T_inv], _T_co]
) -> Callable[[_T_contra], _T_co]:
"""
`g after f` composition of functions (reverse of compose)
"""
return compose(g, f)


def apply_reverse(input: _Tin) -> Callable[[Callable[[_Tin], _Tout]], _Tout]:
def apply_reverse(input: _T_contra) -> Callable[[Callable[[_T_contra], _T_co]], _T_co]:
"""
Applying a function. Equiv curried `(&)` in Haskel
a -> (a -> b) -> b
Expand All @@ -61,15 +65,15 @@ def apply_reverse(input: _Tin) -> Callable[[Callable[[_Tin], _Tout]], _Tout]:
"""

def _intern(func: Callable[[_Tin], _Tout]) -> _Tout:
def _intern(func: Callable[[_T_contra], _T_co]) -> _T_co:
return func(input)

return _intern


def curry2(
func: Callable[[_Tin, _Tin2], _Tout],
) -> Callable[[_Tin], Callable[[_Tin2], _Tout]]:
func: Callable[[_T_contra, _T2_contra], _T_co],
) -> Callable[[_T_contra], Callable[[_T2_contra], _T_co]]:
"""
Transforms 2-param function into two nested 1-param functions
Expand All @@ -82,8 +86,8 @@ def curry2(
"""

def _intern(input: _Tin) -> Callable[[_Tin2], _Tout]:
def _intern2(input2: _Tin2) -> _Tout:
def _intern(input: _T_contra) -> Callable[[_T2_contra], _T_co]:
def _intern2(input2: _T2_contra) -> _T_co:
return func(input, input2)

return _intern2
Expand All @@ -92,8 +96,8 @@ def _intern2(input2: _Tin2) -> _Tout:


def uncurry2(
func: Callable[[_Tin], Callable[[_Tin2], _Tout]],
) -> Callable[[_Tin, _Tin2], _Tout]:
func: Callable[[_T_contra], Callable[[_T2_contra], _T_co]],
) -> Callable[[_T_contra, _T2_contra], _T_co]:
"""
Transforms two nested 1-param functions into one 2-param function
Expand All @@ -109,18 +113,18 @@ def uncurry2(
"""

def _intern(input: _Tin, input2: _Tin2) -> _Tout:
def _intern(input: _T_contra, input2: _T2_contra) -> _T_co:
return func(input)(input2)

return _intern


def curry3(
func: Callable[[_Tin, _Tin2, _Tin3], _Tout],
) -> Callable[[_Tin], Callable[[_Tin2], Callable[[_Tin3], _Tout]]]:
def _intern(input: _Tin) -> Callable[[_Tin2], Callable[[_Tin3], _Tout]]:
def _intern2(input2: _Tin2) -> Callable[[_Tin3], _Tout]:
def _intern3(input3: _Tin3) -> _Tout:
func: Callable[[_T_contra, _T2_contra, _T3_contra], _T_co],
) -> Callable[[_T_contra], Callable[[_T2_contra], Callable[[_T3_contra], _T_co]]]:
def _intern(input: _T_contra) -> Callable[[_T2_contra], Callable[[_T3_contra], _T_co]]:
def _intern2(input2: _T2_contra) -> Callable[[_T3_contra], _T_co]:
def _intern3(input3: _T3_contra) -> _T_co:
return func(input, input2, input3)

return _intern3
Expand All @@ -130,23 +134,25 @@ def _intern3(input3: _Tin3) -> _Tout:
return _intern


def fst(t: Tuple[_Tin, _Tin2]) -> _Tin:
def fst(t: Tuple[_T_inv, _T2_contra]) -> _T_inv:
"""get first of tuple
>>> fst((1, 2)) == 1
True
"""
return t[0]


def snd(t: Tuple[_Tin, _Tin2]) -> _Tin2:
def snd(t: Tuple[_T_contra, _T_inv]) -> _T_inv:
"""get second of tuple
>>> snd((1, 2)) == 2
True
"""
return t[1]


def flip(func: Callable[[_Tin, _Tin2], _Tout]) -> Callable[[_Tin2, _Tin], _Tout]:
def flip(
func: Callable[[_T_contra, _T2_contra], _T_co],
) -> Callable[[_T2_contra, _T_contra], _T_co]:
"""flips params
>>> def minus(a: int, b: int) -> int:
... return a - b
Expand All @@ -156,31 +162,35 @@ def flip(func: Callable[[_Tin, _Tin2], _Tout]) -> Callable[[_Tin2, _Tin], _Tout]
True
"""

def _intern(input2: _Tin2, input: _Tin) -> _Tout:
def _intern(input2: _T2_contra, input: _T_contra) -> _T_co:
return func(input, input2)

return _intern


def compact2(func: Callable[[_Tin, _Tin2], _Tout]) -> Callable[[Tuple[_Tin, _Tin2]], _Tout]:
def compact2(
func: Callable[[_T_contra, _T2_contra], _T_co],
) -> Callable[[Tuple[_T_contra, _T2_contra]], _T_co]:
"""Compacts two parameters into one tuple"""

def _intern(input: Tuple[_Tin, _Tin2]) -> _Tout:
def _intern(input: Tuple[_T_contra, _T2_contra]) -> _T_co:
return func(fst(input), snd(input))

return _intern


def expand2(func: Callable[[Tuple[_Tin, _Tin2]], _Tout]) -> Callable[[_Tin, _Tin2], _Tout]:
def _intern(input: _Tin, input2: _Tin2) -> _Tout:
def expand2(
func: Callable[[Tuple[_T_contra, _T2_contra]], _T_co],
) -> Callable[[_T_contra, _T2_contra], _T_co]:
def _intern(input: _T_contra, input2: _T2_contra) -> _T_co:
return func((input, input2))

return _intern


def pipe2(
f: Callable[[_Tin, _Tin2], _Tinter], g: Callable[[_Tinter], _Tout]
) -> Callable[[_Tin, _Tin2], _Tout]:
f: Callable[[_T_contra, _T2_contra], _T_inv], g: Callable[[_T_inv], _T_co]
) -> Callable[[_T_contra, _T2_contra], _T_co]:
"""
`g after f` composition of functions (reverse of compose). f takes 2 params
>>> def mul(a: int, b: int) -> int:
Expand Down

0 comments on commit 797c584

Please sign in to comment.