Some¶
Bases: Option[T]
Option variant representing the presence of a value.
Attributes:
| Name | Type | Description |
|---|---|---|
value |
T
|
The contained value. |
Example:
>>> import pyochain as pc
>>> pc.Some(42)
Some(42)
Source code in src/pyochain/_results/_option.py
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 | |
and_ ¶
and_(optb: Option[U]) -> Option[U]
Returns NONE if the option is NONE, otherwise returns optb.
This is similar to and_then, except that the value is passed directly instead of through a closure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optb
|
Option[U]
|
The option to return if the original option is |
required |
Returns:
Option[U]: NONE if the original option is NONE, otherwise optb.
Example:
>>> import pyochain as pc
>>> pc.Some(2).and_(pc.NONE)
NONE
>>> pc.NONE.and_(pc.Some("foo"))
NONE
>>> pc.Some(2).and_(pc.Some("foo"))
Some('foo')
>>> pc.NONE.and_(pc.NONE)
NONE
Source code in src/pyochain/_results/_option.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
and_then ¶
and_then(
f: Callable[Concatenate[T, P], Option[R]], *args: P.args, **kwargs: P.kwargs
) -> Option[R]
Calls a function if the option is Some, otherwise returns None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[Concatenate[T, P], Option[R]]
|
The function to call with the |
required |
*args
|
P.args
|
Additional positional arguments to pass to f. |
()
|
**kwargs
|
P.kwargs
|
Additional keyword arguments to pass to f. |
{}
|
Returns:
| Type | Description |
|---|---|
Option[R]
|
Option[R]: The result of the function if |
Example:
>>> import pyochain as pc
>>> def sq(x: int) -> Option[int]:
... return pc.Some(x * x)
>>> def nope(x: int) -> Option[int]:
... return pc.NONE
>>> pc.Some(2).and_then(sq).and_then(sq)
Some(16)
>>> pc.Some(2).and_then(sq).and_then(nope)
NONE
>>> pc.Some(2).and_then(nope).and_then(sq)
NONE
>>> pc.NONE.and_then(sq).and_then(sq)
NONE
Source code in src/pyochain/_results/_option.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
expect ¶
expect(msg: str) -> T
Returns the contained Some value.
Raises an exception with a provided message if the value is None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
str
|
The message to include in the exception if the result is |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
The contained |
Raises:
| Type | Description |
|---|---|
OptionUnwrapError
|
If the result is |
Example:
>>> import pyochain as pc
>>> pc.Some("value").expect("fruits are healthy")
'value'
>>> pc.NONE.expect("fruits are healthy")
Traceback (most recent call last):
...
pyochain._results._option.OptionUnwrapError: fruits are healthy (called `expect` on a `None`)
Source code in src/pyochain/_results/_option.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
filter ¶
filter(
predicate: Callable[Concatenate[T, P], R], *args: P.args, **kwargs: P.kwargs
) -> Option[T]
Returns None if the option is None, otherwise calls predicate with the wrapped value.
This function works similar to Iter.filter in the sense that we only keep the value if it matches a predicate.
You can imagine the Option[T] being an iterator over one or zero elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[Concatenate[T, P], R]
|
The predicate to apply to the contained value. |
required |
*args
|
P.args
|
Additional positional arguments to pass to predicate. |
()
|
**kwargs
|
P.kwargs
|
Additional keyword arguments to pass to predicate. |
{}
|
Returns:
| Type | Description |
|---|---|
Option[T]
|
Option[T]: |
Examples:
>>> import pyochain as pc
>>>
>>> def is_even(n: int) -> bool:
... return n % 2 == 0
>>>
>>> pc.NONE.filter(is_even)
NONE
>>> pc.Some(3).filter(is_even)
NONE
>>> pc.Some(4).filter(is_even)
Some(4)
Source code in src/pyochain/_results/_option.py
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | |
flatten ¶
flatten() -> Option[U]
Flattens a nested Option.
Converts an Option[Option[U]] into an Option[U] by removing one level of nesting.
Equivalent to Option.and_then(lambda x: x).
Returns:
| Type | Description |
|---|---|
Option[U]
|
Option[U]: The flattened option. |
Example:
>>> import pyochain as pc
>>> pc.Some(pc.Some(42)).flatten()
Some(42)
>>> pc.Some(pc.NONE).flatten()
NONE
>>> pc.NONE.flatten()
NONE
Source code in src/pyochain/_results/_option.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
from_
staticmethod
¶
from_(value: V | None) -> Option[V]
Creates an Option[V] from a value that may be None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
V | None
|
The value to convert into an |
required |
Returns:
| Type | Description |
|---|---|
Option[V]
|
Option[V]: |
Example:
>>> import pyochain as pc
>>> pc.Option.from_(42)
Some(42)
>>> pc.Option.from_(None)
NONE
Source code in src/pyochain/_results/_option.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
inspect ¶
inspect(f: Callable[[T], object]) -> Option[T]
Applies a function to the contained Some value, returning the original Option.
This mirrors :meth:Result.inspect, allowing side effects
(logging, debugging, metrics, etc.) on the wrapped value without changing it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[T], object]
|
Function to apply to the |
required |
Returns:
| Type | Description |
|---|---|
Option[T]
|
Option[T]: The original option, unchanged. |
Example:
>>> import pyochain as pc
>>> seen: list[int] = []
>>> pc.Some(2).inspect(lambda x: seen.append(x))
Some(2)
>>> seen
[2]
>>> pc.NONE.inspect(lambda x: seen.append(x))
NONE
>>> seen
[2]
Source code in src/pyochain/_results/_option.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
into ¶
into(func: Callable[Concatenate[Self, P], R], *args: P.args, **kwargs: P.kwargs) -> R
Convert Self to R.
This method allows to pipe the instance into an object or function that can convert Self into another type.
Conceptually, this allow to do x.into(f) instead of f(x), hence keeping a functional chaining style.
This is a core method, shared by all pyochain wrappers, that allows chaining operations in a functional style.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[Concatenate[Self, P], R]
|
Function for conversion. |
required |
*args
|
P.args
|
Positional arguments to pass to the function. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the function. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
R |
R
|
The converted value. |
Example:
>>> import pyochain as pc
>>> def maybe_sum(data: pc.Seq[int]) -> pc.Option[int]:
... match data.length():
... case 0:
... return pc.NONE
... case _:
... return pc.Some(data.sum())
>>>
>>> pc.Seq(range(5)).into(maybe_sum).unwrap()
10
Source code in src/pyochain/_core/_main.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
is_none ¶
is_none() -> bool
Source code in src/pyochain/_results/_option.py
868 869 | |
is_none_or ¶
is_none_or(
func: Callable[Concatenate[T, P], bool], *args: P.args, **kwargs: P.kwargs
) -> bool
Returns true if the option is a None or the value inside of it matches a predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[Concatenate[T, P], bool]
|
The predicate to apply to the contained value. |
required |
*args
|
P.args
|
Additional positional arguments to pass to func. |
()
|
**kwargs
|
P.kwargs
|
Additional keyword arguments to pass to func. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Examples:
>>> import pyochain as pc
>>> pc.Some(2).is_none_or(lambda x: x > 1)
True
>>> pc.Some(0).is_none_or(lambda x: x > 1)
False
>>> pc.NONE.is_none_or(lambda x: x > 1)
True
>>> pc.Some("hello").is_none_or(lambda x: len(x) > 1)
True
Source code in src/pyochain/_results/_option.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
is_some ¶
is_some() -> bool
Source code in src/pyochain/_results/_option.py
865 866 | |
is_some_and ¶
is_some_and(
predicate: Callable[Concatenate[T, P], bool], *args: P.args, **kwargs: P.kwargs
) -> bool
Returns true if the option is a Some and the value inside of it matches a predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[Concatenate[T, P], bool]
|
The predicate to apply to the contained value. |
required |
*args
|
P.args
|
Additional positional arguments to pass to predicate. |
()
|
**kwargs
|
P.kwargs
|
Additional keyword arguments to pass to predicate. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Examples:
>>> import pyochain as pc
>>> x = pc.Some(2)
>>> x.is_some_and(lambda x: x > 1)
True
>>> x = pc.Some(0)
>>> x.is_some_and(lambda x: x > 1)
False
>>> x = pc.NONE
>>> x.is_some_and(lambda x: x > 1)
False
>>> x = pc.Some("hello")
>>> x.is_some_and(lambda x: len(x) > 1)
True
Source code in src/pyochain/_results/_option.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
iter ¶
iter() -> Iter[T]
Creates an Iter over the optional value.
- If the option is
Some(value), the iterator yieldsvalue. - If the option is
NONE, the iterator yields nothing.
Equivalent to Iter((self,)).
Returns:
| Type | Description |
|---|---|
Iter[T]
|
Iter[T]: An iterator over the optional value. |
Example:
>>> import pyochain as pc
>>> pc.Some(42).iter().next()
Some(42)
>>> pc.NONE.iter().next()
NONE
Source code in src/pyochain/_results/_option.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | |
map ¶
map(f: Callable[Concatenate[T, P], R], *args: P.args, **kwargs: P.kwargs) -> Option[R]
Maps an Option[T] to Option[U].
Done by applying a function to a contained Some value,
leaving a None value untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[Concatenate[T, P], R]
|
The function to apply to the |
required |
*args
|
P.args
|
Additional positional arguments to pass to f. |
()
|
**kwargs
|
P.kwargs
|
Additional keyword arguments to pass to f. |
{}
|
Returns:
| Type | Description |
|---|---|
Option[R]
|
Option[R]: A new |
Example:
>>> import pyochain as pc
>>> pc.Some("Hello, World!").map(len)
Some(13)
>>> pc.NONE.map(len)
NONE
Source code in src/pyochain/_results/_option.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
map_or ¶
map_or(
default: R, f: Callable[Concatenate[T, P], R], *args: P.args, **kwargs: P.kwargs
) -> R
Returns the result of applying a function to the contained value if Some, otherwise returns the default value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
R
|
The default value to return if NONE. |
required |
f
|
Callable[Concatenate[T, P], R]
|
The function to apply to the contained value. |
required |
*args
|
P.args
|
Additional positional arguments to pass to f. |
()
|
**kwargs
|
P.kwargs
|
Additional keyword arguments to pass to f. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
R |
R
|
The result of f(self.unwrap()) if Some, otherwise default. |
Example:
>>> import pyochain as pc
>>> pc.Some(2).map_or(0, lambda x: x * 10)
20
>>> pc.NONE.map_or(0, lambda x: x * 10)
0
Source code in src/pyochain/_results/_option.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |
map_or_else ¶
map_or_else(default: Callable[[], R], f: Callable[[T], R]) -> R
Returns the result of applying a function to the contained value if Some, otherwise computes a default value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Callable[[], R]
|
A function returning the default value if NONE. |
required |
f
|
Callable[[T], R]
|
The function to apply to the contained value. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
R |
R
|
The result of f(self.unwrap()) if Some, otherwise default(). |
Example:
>>> import pyochain as pc
>>> pc.Some(2).map_or_else(lambda: 0, lambda x: x * 10)
20
>>> pc.NONE.map_or_else(lambda: 0, lambda x: x * 10)
0
Source code in src/pyochain/_results/_option.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | |
ok_or ¶
ok_or(err: E) -> Result[T, E]
Converts the option to a Result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
err
|
E
|
The error value to use if the option is |
required |
Returns:
| Type | Description |
|---|---|
Result[T, E]
|
Result[T, E]: |
Example:
>>> import pyochain as pc
>>> pc.Some(1).ok_or('fail')
Ok(1)
>>> pc.NONE.ok_or('fail')
Err('fail')
Source code in src/pyochain/_results/_option.py
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | |
ok_or_else ¶
ok_or_else(err: Callable[[], E]) -> Result[T, E]
Converts the option to a Result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
err
|
Callable[[], E]
|
A function returning the error value if the option is NONE. |
required |
Returns:
| Type | Description |
|---|---|
Result[T, E]
|
Result[T, E]: Ok(v) if Some(v), otherwise Err(err()). |
Example:
>>> import pyochain as pc
>>> pc.Some(1).ok_or_else(lambda: 'fail')
Ok(1)
>>> pc.NONE.ok_or_else(lambda: 'fail')
Err('fail')
Source code in src/pyochain/_results/_option.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
or_ ¶
or_(optb: Option[T]) -> Option[T]
Returns the option if it contains a value, otherwise returns optb.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optb
|
Option[T]
|
The option to return if the original option is |
required |
Returns:
| Type | Description |
|---|---|
Option[T]
|
Option[T]: The original option if it is |
Examples:
>>> import pyochain as pc
>>> pc.Some(2).or_(pc.NONE)
Some(2)
>>> pc.NONE.or_(pc.Some(100))
Some(100)
>>> pc.Some(2).or_(pc.Some(100))
Some(2)
>>> pc.NONE.or_(pc.NONE)
NONE
Source code in src/pyochain/_results/_option.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
or_else ¶
or_else(f: Callable[[], Option[T]]) -> Option[T]
Returns the Option[T] if it contains a value, otherwise calls a function and returns the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[], Option[T]]
|
The function to call if the option is |
required |
Returns:
| Type | Description |
|---|---|
Option[T]
|
Option[T]: The original |
Example:
>>> import pyochain as pc
>>> def nobody() -> Option[str]:
... return pc.NONE
>>> def vikings() -> Option[str]:
... return pc.Some("vikings")
>>> pc.Some("barbarians").or_else(vikings)
Some('barbarians')
>>> pc.NONE.or_else(vikings)
Some('vikings')
>>> pc.NONE.or_else(nobody)
NONE
Source code in src/pyochain/_results/_option.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
reduce ¶
reduce(other: Option[T], func: Callable[[T, T], T]) -> Option[T]
Reduces two options into one, using the provided function if both are Some.
If self is Some(s) and other is Some(o), this method returns Some(func(s, o)).
Otherwise, if only one of self and other is Some, that value is returned.
If both self and other are NONE, NONE is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Option[T]
|
The second option. |
required |
func
|
Callable[[T, T], T]
|
The function to apply to the unwrapped values. |
required |
Returns:
| Type | Description |
|---|---|
Option[T]
|
Option[T]: The resulting option after reduction. |
Examples:
>>> import pyochain as pc
>>> s12 = pc.Some(12)
>>> s17 = pc.Some(17)
>>>
>>> def add(a: int, b: int) -> int:
... return a + b
>>>
>>> s12.reduce(s17, add)
Some(29)
>>> s12.reduce(pc.NONE, add)
Some(12)
>>> pc.NONE.reduce(s17, add)
Some(17)
>>> pc.NONE.reduce(pc.NONE, add)
NONE
Source code in src/pyochain/_results/_option.py
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 | |
tap ¶
tap(
func: Callable[Concatenate[Self, P], Any], *args: P.args, **kwargs: P.kwargs
) -> Self
Tap into the chain to perform side effects without altering the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[Concatenate[Self, P], Any]
|
Function to apply to the instance for side effects. |
required |
*args
|
P.args
|
Positional arguments to pass to the function. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the function. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The instance itself for chaining. |
Example:
>>> import pyochain as pc
>>> pc.Seq([1, 2, 3, 4]).tap(print).last()
Seq(1, 2, 3, 4)
4
Source code in src/pyochain/_core/_main.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
transpose ¶
transpose() -> Result[Option[T], E]
Transposes an Option of a Result into a Result of an Option.
Some(Ok[T]) is mapped to Ok(Some[T]), Some(Err[E]) is mapped to Err[E], and NONE will be mapped to Ok(NONE).
Returns:
| Type | Description |
|---|---|
Result[Option[T], E]
|
Result[Option[T], E]: The transposed result. |
Examples:
>>> import pyochain as pc
>>> pc.Some(pc.Ok(5)).transpose()
Ok(Some(5))
>>> pc.NONE.transpose()
Ok(NONE)
>>> pc.Some(pc.Err("error")).transpose()
Err('error')
Source code in src/pyochain/_results/_option.py
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 | |
unwrap ¶
unwrap() -> T
Source code in src/pyochain/_results/_option.py
871 872 | |
unwrap_or ¶
unwrap_or(default: T) -> T
Returns the contained Some value or a provided default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
T
|
The value to return if the result is |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
The contained |
Example:
>>> import pyochain as pc
>>> pc.Some("car").unwrap_or("bike")
'car'
>>> pc.NONE.unwrap_or("bike")
'bike'
Source code in src/pyochain/_results/_option.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
unwrap_or_else ¶
unwrap_or_else(f: Callable[[], T]) -> T
Returns the contained Some value or computes it from a function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[], T]
|
A function that returns a default value if the result is |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
The contained |
Example:
>>> import pyochain as pc
>>> k = 10
>>> pc.Some(4).unwrap_or_else(lambda: 2 * k)
4
>>> pc.NONE.unwrap_or_else(lambda: 2 * k)
20
Source code in src/pyochain/_results/_option.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
unzip ¶
unzip() -> tuple[Option[T], Option[U]]
Unzips an Option of a tuple into a tuple of Options.
If the option is Some((a, b)), this method returns (Some(a), Some(b)).
If the option is NONE, it returns (NONE, NONE).
Returns:
| Type | Description |
|---|---|
tuple[Option[T], Option[U]]
|
tuple[Option[T], Option[U]]: A tuple containing two options. |
Example:
>>> import pyochain as pc
>>> pc.Some((1, 'a')).unzip()
(Some(1), Some('a'))
>>> pc.NONE.unzip()
(NONE, NONE)
Source code in src/pyochain/_results/_option.py
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 | |
xor ¶
xor(optb: Option[T]) -> Option[T]
Returns Some if exactly one of self, optb is Some, otherwise returns NONE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optb
|
Option[T]
|
The other option to compare with. |
required |
Returns:
| Type | Description |
|---|---|
Option[T]
|
Option[T]: |
Examples:
>>> import pyochain as pc
>>> pc.Some(2).xor(pc.NONE)
Some(2)
>>> pc.NONE.xor(pc.Some(2))
Some(2)
>>> pc.Some(2).xor(pc.Some(2))
NONE
>>> pc.NONE.xor(pc.NONE)
NONE
Source code in src/pyochain/_results/_option.py
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 | |
zip ¶
zip(other: Option[U]) -> Option[tuple[T, U]]
Returns an Option[tuple[T, U]] containing a tuple of the values if both options are Some, otherwise returns NONE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Option[U]
|
The other option to zip with. |
required |
Returns:
| Type | Description |
|---|---|
Option[tuple[T, U]]
|
Option[tuple[T, U]]: Some((self, other)) if both are Some, otherwise NONE. |
Example:
>>> import pyochain as pc
>>> pc.Some(1).zip(pc.Some('a'))
Some((1, 'a'))
>>> pc.Some(1).zip(pc.NONE)
NONE
>>> pc.NONE.zip(pc.Some('a'))
NONE
Source code in src/pyochain/_results/_option.py
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 | |
zip_with ¶
zip_with(other: Option[U], f: Callable[[T, U], R]) -> Option[R]
Zips self and another Option with function f.
If self is Some(s) and other is Some(o), this method returns Some(f(s, o)).
Otherwise, NONE is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Option[U]
|
The second option. |
required |
f
|
Callable[[T, U], R]
|
The function to apply to the unwrapped values. |
required |
Returns:
| Type | Description |
|---|---|
Option[R]
|
Option[R]: The resulting option after applying the function. |
Examples:
>>> from dataclasses import dataclass
>>> import pyochain as pc
>>>
>>> @dataclass
... class Point:
... x: float
... y: float
>>>
>>> x = pc.Some(17.5)
>>> y = pc.Some(42.7)
>>> x.zip_with(y, Point)
Some(Point(x=17.5, y=42.7))
>>> x.zip_with(pc.NONE, Point)
NONE
Source code in src/pyochain/_results/_option.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 | |