Skip to content

Commit

Permalink
Additional parameters (#28)
Browse files Browse the repository at this point in the history
* add additional prameters

* ai docs

* bump verision

* update docs
  • Loading branch information
mishankov authored Feb 3, 2024
1 parent bc60f04 commit ebbd2aa
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pip install tipe
... .pipe(float) \
... .unwrap()
4.0
>>> Pipe(2).pipe(range, 4).pipe(len).unwrap()
2
```

Equivalent for examples above would be
Expand All @@ -33,6 +35,8 @@ Equivalent for examples above would be
3.0
>>> float(len([2, 3, 4]) + 1)
4.0
>>> len(range(2, 4))
2
```


Expand All @@ -49,7 +53,7 @@ Pipe(2)

### `Pipe.pipe()`

Pass function to execute on `Pipe` value. Wraps function result in `Pipe` and returns it
Pass function to execute on `Pipe` value and additional params for it. Wraps function result in `Pipe` and returns it


### `Pipe.check()`
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = ["Denis Mishankov <mishankov@mail.com>"]
description = "Typed pipe"
name = "tipe"
readme = "README.md"
version = "1.0.0"
version = "1.1.0"

[tool.poetry.dependencies]
python = "^3.8"
Expand Down
8 changes: 8 additions & 0 deletions tests/test_tipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ def test_generator():
Pipe((i**i for i in range(5))).pipe(lambda x: (y for y in x)).pipe(sum).unwrap()
== 289
)


def test_additional_params():
def add(a: int, b: int) -> int:
return a + b

assert Pipe(1).pipe(add, 2).unwrap() == 3
assert Pipe(2).pipe(range, 4).pipe(len).unwrap() == 2
50 changes: 46 additions & 4 deletions tipe/tipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,65 @@

class Pipe(Generic[ValueType]):
def __init__(self, initial_value: ValueType) -> None:
"""
Initializes the object with the initial value and sets up aliases for pipe, check, and unwrap functions.
:param initial_value: The initial value to be assigned to the object.
:type initial_value: ValueType
:return: None
"""
self._value: ValueType = initial_value
self.p = self.pipe
self.c = self.check
self.u = self.unwrap

def pipe(self, function: Callable[[ValueType], FunctionReturnType]):
result: FunctionReturnType = function(self._value)
def pipe(
self,
function: Callable[[ValueType], FunctionReturnType],
*additional_params: list,
):
"""
Applies a function to the value of the Pipe object and returns a new Pipe object.
Args:
function (Callable[[ValueType], FunctionReturnType]): The function to apply to the value.
*additional_params (list): Additional parameters to pass to the function.
Returns:
Pipe: A new Pipe object with the result of applying the function to the value.
"""
result = function(self._value, *additional_params)

return Pipe(result)

def check(self, function: Callable[[ValueType], FunctionReturnType]):
function(self._value)
def check(
self,
function: Callable[[ValueType], FunctionReturnType],
*additional_params: list,
):
"""
Apply the given function to the current value and return the result as a new Pipe.
Args:
function: The function to apply to the current value.
*additional_params: Additional parameters to pass to the function.
Returns:
Pipe: A new Pipe containing the result of applying the function to the current value.
"""
function(self._value, *additional_params)
return Pipe(self._value)

def unwrap(self):
"""
Return the value of the wrapped object.
"""
return self._value

def __repr__(self) -> str:
"""
Return a string representation of the Pipe object.
"""
return f"Pipe({self._value})"


Expand Down

0 comments on commit ebbd2aa

Please sign in to comment.