Skip to content

Commit

Permalink
feat: Experimental work on issue #50
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Jan 6, 2023
1 parent 7c61a9d commit f0f2dd1
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions i2/scrap/signature_bops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Signature binary operators
See design issue: https://github.com/i2mint/i2/issues/50
"""

from functools import partial

from typing import Callable, TypeVar, Optional, Union

B = TypeVar('B')
B.__doc__ = (
"A 'base' type that we are able (example, builtin object) to operate on, as is"
)
A = TypeVar('A')
A.__doc__ = "The 'abstract' type we want to operate on through a key-function"
KeyFunction = Callable[[A], B]
KeyFunction.__doc__ = 'Function that transforms an A to a B'

AorB = TypeVar('AorB', A, B)
AorB.__doc__ = 'An A (abstract) or a B (base)'
AB = TypeVar('AB', bound=AorB)
AorB.__doc__ = 'A generic AorB'
R = TypeVar('R')
R.__doc__ = 'The return type of a binary operator'

BaseBinaryOperator = Callable[[B, B], R]
AbstractBinaryOperator = Callable[[A, A], R]
# BinaryOperator = Union[BaseBinaryOperator, AbstractBinaryOperator] # equivalent?
BinaryOperator = Callable[[AB, AB], R]
KeyEnabledBinaryOperator = Callable[[AB, AB, KeyFunction], R]


def key_function_enabled_operator(
binary_operator: Union[BaseBinaryOperator, AbstractBinaryOperator],
x: AB,
y: AB, # has to be the same as x. If x is A, so should y, if x is B so should y
key: Optional[KeyFunction] = None,
) -> R:
if key is None:
return binary_operator(x, y)
else:
return binary_operator(key(x), key(y))


def key_function_factory(binary_operator: BinaryOperator) -> KeyEnabledBinaryOperator:
return partial(key_function_enabled_operator, binary_operator)

0 comments on commit f0f2dd1

Please sign in to comment.