Skip to content

trcks.oop.Wrapper

Bases: _Wrapper[_T_co]

Type-safe and immutable wrapper for arbitrary objects.

The wrapped object can be accessed via the attribute trcks.oop.Wrapper.core. The trcks.oop.Wrapper.map* methods allow method chaining. The trcks.oop.Wrapper.tap* methods allow for side effects without changing the wrapped object.

Example

The string "Hello" is wrapped and manipulated in the following example. Finally, the result is unwrapped:

>>> wrapper = (
...     Wrapper(core="Hello")
...     .map(len)
...     .tap(lambda n: print(f"Received {n}."))
...     .map(lambda n: f"Length: {n}")
... )
Received 5.
>>> wrapper
Wrapper(core='Length: 5')
>>> wrapper.core
'Length: 5'
Source code in src/trcks/oop.py
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
class Wrapper(_Wrapper[_T_co]):
    """Type-safe and immutable wrapper for arbitrary objects.

    The wrapped object can be accessed via the attribute `trcks.oop.Wrapper.core`.
    The `trcks.oop.Wrapper.map*` methods allow method chaining.
    The `trcks.oop.Wrapper.tap*` methods allow for side effects
    without changing the wrapped object.

    Example:
        The string `"Hello"` is wrapped and manipulated in the following example.
        Finally, the result is unwrapped:

        >>> wrapper = (
        ...     Wrapper(core="Hello")
        ...     .map(len)
        ...     .tap(lambda n: print(f"Received {n}."))
        ...     .map(lambda n: f"Length: {n}")
        ... )
        Received 5.
        >>> wrapper
        Wrapper(core='Length: 5')
        >>> wrapper.core
        'Length: 5'
    """

    @staticmethod
    def construct(value: _T) -> Wrapper[_T]:
        """Alias for the default constructor.

        Args:
            value: The object to be wrapped.

        Returns:
            A new [trcks.oop.Wrapper][] instance with the wrapped object.

        Example:
            >>> Wrapper.construct(5)
            Wrapper(core=5)
        """
        return Wrapper(core=value)

    def map(self, f: Callable[[_T_co], _T]) -> Wrapper[_T]:
        """Apply a synchronous function to the wrapped object.

        Args:
            f: The synchronous function to be applied.

        Returns:
            A new [trcks.oop.Wrapper][] instance
                with the result of the function application.

        Example:
            >>> Wrapper.construct(5).map(lambda n: f"The number is {n}.")
            Wrapper(core='The number is 5.')
        """
        return Wrapper(f(self.core))

    def map_to_awaitable(
        self, f: Callable[[_T_co], Awaitable[_T]]
    ) -> AwaitableWrapper[_T]:
        """Apply an asynchronous function to the wrapped object.

        Args:
            f: The asynchronous function to be applied.

        Returns:
            A [trcks.oop.AwaitableWrapper][] instance with
                the result of the function application.

        Example:
            >>> import asyncio
            >>> from trcks.oop import Wrapper
            >>> async def stringify_slowly(o: object) -> str:
            ...     await asyncio.sleep(0.001)
            ...     return str(o)
            ...
            >>> awaitable_wrapper = Wrapper.construct(3.14).map_to_awaitable(
            ...     stringify_slowly
            ... )
            >>> awaitable_wrapper
            AwaitableWrapper(core=<coroutine object stringify_slowly at 0x...>)
            >>> asyncio.run(awaitable_wrapper.core_as_coroutine)
            '3.14'
        """
        return AwaitableWrapper(f(self.core))

    def map_to_awaitable_result(
        self, f: Callable[[_T_co], AwaitableResult[_F, _S]]
    ) -> AwaitableResultWrapper[_F, _S]:
        """Apply an asynchronous function with return type [trcks.Result][]
        to the wrapped object.

        Args:
            f: The asynchronous function to be applied.

        Returns:
            A [trcks.oop.AwaitableResultWrapper][] instance with
                the result of the function application.

        Example:
            >>> import asyncio
            >>> from trcks import Result
            >>> from trcks.oop import Wrapper
            >>> async def slowly_assert_non_negative(
            ...     x: float,
            ... ) -> Result[str, float]:
            ...     await asyncio.sleep(0.001)
            ...     if x < 0:
            ...         return "failure", "negative value"
            ...     return "success", x
            ...
            >>> awaitable_result_wrapper = (
            ...     Wrapper
            ...     .construct(42.0)
            ...     .map_to_awaitable_result(slowly_assert_non_negative)
            ... )
            >>> awaitable_result_wrapper
            AwaitableResultWrapper(core=<coroutine object ...>)
            >>> asyncio.run(awaitable_result_wrapper.core_as_coroutine)
            ('success', 42.0)
        """
        return AwaitableResultWrapper(f(self.core))

    def map_to_result(
        self, f: Callable[[_T_co], Result[_F, _S]]
    ) -> ResultWrapper[_F, _S]:
        """Apply a synchronous function with return type [trcks.Result][]
        to the wrapped object.

        Args:
            f: The synchronous function to be applied.

        Returns:
            A [trcks.oop.ResultWrapper][] instance with
                the result of the function application.

        Example:
            >>> Wrapper.construct(-1).map_to_result(
            ...     lambda x: ("success", x)
            ...     if x >= 0
            ...     else ("failure", "negative value")
            ... )
            ResultWrapper(core=('failure', 'negative value'))
        """
        return ResultWrapper(f(self.core))

    def tap(self, f: Callable[[_T_co], object]) -> Wrapper[_T_co]:
        """Apply a synchronous side effect to the wrapped object.

        Args:
            f: The synchronous side effect to be applied.

        Returns:
            A new [trcks.oop.Wrapper][] instance with the original wrapped object,
                allowing for further method chaining.

        Example:
            >>> wrapper = Wrapper.construct(5).tap(lambda n: print(f"Number: {n}"))
            Number: 5
            >>> wrapper
            Wrapper(core=5)
        """
        return self.map(i.tap(f))

    def tap_to_awaitable(
        self, f: Callable[[_T_co], Awaitable[object]]
    ) -> AwaitableWrapper[_T_co]:
        """Apply an asynchronous side effect to the wrapped object.

        Args:
            f: The asynchronous side effect to be applied.

        Returns:
            A [trcks.oop.AwaitableWrapper][] instance with the original wrapped object.

        Example:
            >>> import asyncio
            >>> from trcks.oop import Wrapper
            >>> async def write_to_disk(s: str) -> None:
            ...     await asyncio.sleep(0.001)
            ...     print(f"Wrote '{s}' to disk.")
            ...
            >>> awaitable_wrapper = Wrapper.construct(
            ...     "Hello, world!"
            ... ).tap_to_awaitable(write_to_disk)
            >>> awaitable_wrapper
            AwaitableWrapper(core=<coroutine object ...>)
            >>> value = asyncio.run(awaitable_wrapper.core_as_coroutine)
            Wrote 'Hello, world!' to disk.
            >>> value
            'Hello, world!'
        """
        return AwaitableWrapper.construct(self.core).tap_to_awaitable(f)

    def tap_to_awaitable_result(
        self, f: Callable[[_T_co], AwaitableResult[_F, object]]
    ) -> AwaitableResultWrapper[_F, _T_co]:
        """Apply an asynchronous side effect with return type [trcks.Result][]
        to the wrapped object.

        Args:
            f: The asynchronous side effect to be applied.

        Returns:
            A [trcks.oop.AwaitableResultWrapper][] instance with

                - *the returned* [trcks.Failure][]
                    if the given side effect returns a [trcks.Failure][] or
                - a [trcks.Success][] instance containing *the original* wrapped object
                    if the given side effect returns a [trcks.Success][].

        Example:
            >>> import asyncio
            >>> from trcks import Result
            >>> from trcks.oop import Wrapper
            >>> async def write_to_disk(s: str, path: str) -> Result[str, None]:
            ...     if path != "output.txt":
            ...         return "failure", "write error"
            ...     await asyncio.sleep(0.001)
            ...     print(f"Wrote '{s}' to file {path}.")
            ...     return "success", None
            ...
            >>> awaitable_result_wrapper_1 = Wrapper.construct(
            ...     "Hello, world!"
            ... ).tap_to_awaitable_result(
            ...     lambda s: write_to_disk(s, "destination.txt")
            ... )
            >>> awaitable_result_wrapper_1
            AwaitableResultWrapper(core=<coroutine object ...>)
            >>> result_1 = asyncio.run(awaitable_result_wrapper_1.core_as_coroutine)
            >>> result_1
            ('failure', 'write error')
            >>> awaitable_result_wrapper_2 = Wrapper.construct(
            ...     "Hello, world!"
            ... ).tap_to_awaitable_result(
            ...     lambda s: write_to_disk(s, "output.txt")
            ... )
            >>> awaitable_result_wrapper_2
            AwaitableResultWrapper(core=<coroutine object ...>)
            >>> result_2 = asyncio.run(awaitable_result_wrapper_2.core_as_coroutine)
            Wrote 'Hello, world!' to file output.txt.
            >>> result_2
            ('success', 'Hello, world!')
        """
        return AwaitableResultWrapper.construct_success(
            self.core
        ).tap_success_to_awaitable_result(f)

    def tap_to_result(
        self, f: Callable[[_T_co], Result[_F, object]]
    ) -> ResultWrapper[_F, _T_co]:
        """Apply a synchronous side effect with return type [trcks.Result][]
        to the wrapped object.

        Args:
            f: The synchronous side effect to be applied.

        Returns:
            A [trcks.oop.ResultWrapper][] instance with

                - *the returned* [trcks.Failure][]
                    if the given side effect returns a [trcks.Failure][] or
                - a [trcks.Success][] instance containing *the original* wrapped object
                    if the given side effect returns a [trcks.Success][].

        Example:
            >>> from trcks import Result
            >>> from trcks.oop import Wrapper
            >>> def print_positive_float(x: float) -> Result[str, None]:
            ...     if x <= 0:
            ...         return "failure", "not positive"
            ...     return "success", print(f"Positive float: {x}")
            ...
            >>> result_wrapper_1 = Wrapper.construct(-2.3).tap_to_result(
            ...     print_positive_float
            ... )
            >>> result_wrapper_1
            ResultWrapper(core=('failure', 'not positive'))
            >>> result_wrapper_2 = Wrapper.construct(3.5).tap_to_result(
            ...     print_positive_float
            ... )
            Positive float: 3.5
            >>> result_wrapper_2
            ResultWrapper(core=('success', 3.5))
        """
        return ResultWrapper.construct_success(self.core).tap_success_to_result(f)

construct(value) staticmethod

Alias for the default constructor.

Parameters:

  • value (_T) –

    The object to be wrapped.

Returns:

Example
>>> Wrapper.construct(5)
Wrapper(core=5)
Source code in src/trcks/oop.py
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
@staticmethod
def construct(value: _T) -> Wrapper[_T]:
    """Alias for the default constructor.

    Args:
        value: The object to be wrapped.

    Returns:
        A new [trcks.oop.Wrapper][] instance with the wrapped object.

    Example:
        >>> Wrapper.construct(5)
        Wrapper(core=5)
    """
    return Wrapper(core=value)

map(f)

Apply a synchronous function to the wrapped object.

Parameters:

  • f (Callable[[_T_co], _T]) –

    The synchronous function to be applied.

Returns:

Example
>>> Wrapper.construct(5).map(lambda n: f"The number is {n}.")
Wrapper(core='The number is 5.')
Source code in src/trcks/oop.py
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
def map(self, f: Callable[[_T_co], _T]) -> Wrapper[_T]:
    """Apply a synchronous function to the wrapped object.

    Args:
        f: The synchronous function to be applied.

    Returns:
        A new [trcks.oop.Wrapper][] instance
            with the result of the function application.

    Example:
        >>> Wrapper.construct(5).map(lambda n: f"The number is {n}.")
        Wrapper(core='The number is 5.')
    """
    return Wrapper(f(self.core))

map_to_awaitable(f)

Apply an asynchronous function to the wrapped object.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks.oop import Wrapper
>>> async def stringify_slowly(o: object) -> str:
...     await asyncio.sleep(0.001)
...     return str(o)
...
>>> awaitable_wrapper = Wrapper.construct(3.14).map_to_awaitable(
...     stringify_slowly
... )
>>> awaitable_wrapper
AwaitableWrapper(core=<coroutine object stringify_slowly at 0x...>)
>>> asyncio.run(awaitable_wrapper.core_as_coroutine)
'3.14'
Source code in src/trcks/oop.py
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
def map_to_awaitable(
    self, f: Callable[[_T_co], Awaitable[_T]]
) -> AwaitableWrapper[_T]:
    """Apply an asynchronous function to the wrapped object.

    Args:
        f: The asynchronous function to be applied.

    Returns:
        A [trcks.oop.AwaitableWrapper][] instance with
            the result of the function application.

    Example:
        >>> import asyncio
        >>> from trcks.oop import Wrapper
        >>> async def stringify_slowly(o: object) -> str:
        ...     await asyncio.sleep(0.001)
        ...     return str(o)
        ...
        >>> awaitable_wrapper = Wrapper.construct(3.14).map_to_awaitable(
        ...     stringify_slowly
        ... )
        >>> awaitable_wrapper
        AwaitableWrapper(core=<coroutine object stringify_slowly at 0x...>)
        >>> asyncio.run(awaitable_wrapper.core_as_coroutine)
        '3.14'
    """
    return AwaitableWrapper(f(self.core))

map_to_awaitable_result(f)

Apply an asynchronous function with return type trcks.Result to the wrapped object.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import Result
>>> from trcks.oop import Wrapper
>>> async def slowly_assert_non_negative(
...     x: float,
... ) -> Result[str, float]:
...     await asyncio.sleep(0.001)
...     if x < 0:
...         return "failure", "negative value"
...     return "success", x
...
>>> awaitable_result_wrapper = (
...     Wrapper
...     .construct(42.0)
...     .map_to_awaitable_result(slowly_assert_non_negative)
... )
>>> awaitable_result_wrapper
AwaitableResultWrapper(core=<coroutine object ...>)
>>> asyncio.run(awaitable_result_wrapper.core_as_coroutine)
('success', 42.0)
Source code in src/trcks/oop.py
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
def map_to_awaitable_result(
    self, f: Callable[[_T_co], AwaitableResult[_F, _S]]
) -> AwaitableResultWrapper[_F, _S]:
    """Apply an asynchronous function with return type [trcks.Result][]
    to the wrapped object.

    Args:
        f: The asynchronous function to be applied.

    Returns:
        A [trcks.oop.AwaitableResultWrapper][] instance with
            the result of the function application.

    Example:
        >>> import asyncio
        >>> from trcks import Result
        >>> from trcks.oop import Wrapper
        >>> async def slowly_assert_non_negative(
        ...     x: float,
        ... ) -> Result[str, float]:
        ...     await asyncio.sleep(0.001)
        ...     if x < 0:
        ...         return "failure", "negative value"
        ...     return "success", x
        ...
        >>> awaitable_result_wrapper = (
        ...     Wrapper
        ...     .construct(42.0)
        ...     .map_to_awaitable_result(slowly_assert_non_negative)
        ... )
        >>> awaitable_result_wrapper
        AwaitableResultWrapper(core=<coroutine object ...>)
        >>> asyncio.run(awaitable_result_wrapper.core_as_coroutine)
        ('success', 42.0)
    """
    return AwaitableResultWrapper(f(self.core))

map_to_result(f)

Apply a synchronous function with return type trcks.Result to the wrapped object.

Parameters:

  • f (Callable[[_T_co], Result[_F, _S]]) –

    The synchronous function to be applied.

Returns:

Example
>>> Wrapper.construct(-1).map_to_result(
...     lambda x: ("success", x)
...     if x >= 0
...     else ("failure", "negative value")
... )
ResultWrapper(core=('failure', 'negative value'))
Source code in src/trcks/oop.py
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
def map_to_result(
    self, f: Callable[[_T_co], Result[_F, _S]]
) -> ResultWrapper[_F, _S]:
    """Apply a synchronous function with return type [trcks.Result][]
    to the wrapped object.

    Args:
        f: The synchronous function to be applied.

    Returns:
        A [trcks.oop.ResultWrapper][] instance with
            the result of the function application.

    Example:
        >>> Wrapper.construct(-1).map_to_result(
        ...     lambda x: ("success", x)
        ...     if x >= 0
        ...     else ("failure", "negative value")
        ... )
        ResultWrapper(core=('failure', 'negative value'))
    """
    return ResultWrapper(f(self.core))

tap(f)

Apply a synchronous side effect to the wrapped object.

Parameters:

  • f (Callable[[_T_co], object]) –

    The synchronous side effect to be applied.

Returns:

  • Wrapper[_T_co]

    A new trcks.oop.Wrapper instance with the original wrapped object, allowing for further method chaining.

Example
>>> wrapper = Wrapper.construct(5).tap(lambda n: print(f"Number: {n}"))
Number: 5
>>> wrapper
Wrapper(core=5)
Source code in src/trcks/oop.py
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
def tap(self, f: Callable[[_T_co], object]) -> Wrapper[_T_co]:
    """Apply a synchronous side effect to the wrapped object.

    Args:
        f: The synchronous side effect to be applied.

    Returns:
        A new [trcks.oop.Wrapper][] instance with the original wrapped object,
            allowing for further method chaining.

    Example:
        >>> wrapper = Wrapper.construct(5).tap(lambda n: print(f"Number: {n}"))
        Number: 5
        >>> wrapper
        Wrapper(core=5)
    """
    return self.map(i.tap(f))

tap_to_awaitable(f)

Apply an asynchronous side effect to the wrapped object.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks.oop import Wrapper
>>> async def write_to_disk(s: str) -> None:
...     await asyncio.sleep(0.001)
...     print(f"Wrote '{s}' to disk.")
...
>>> awaitable_wrapper = Wrapper.construct(
...     "Hello, world!"
... ).tap_to_awaitable(write_to_disk)
>>> awaitable_wrapper
AwaitableWrapper(core=<coroutine object ...>)
>>> value = asyncio.run(awaitable_wrapper.core_as_coroutine)
Wrote 'Hello, world!' to disk.
>>> value
'Hello, world!'
Source code in src/trcks/oop.py
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
def tap_to_awaitable(
    self, f: Callable[[_T_co], Awaitable[object]]
) -> AwaitableWrapper[_T_co]:
    """Apply an asynchronous side effect to the wrapped object.

    Args:
        f: The asynchronous side effect to be applied.

    Returns:
        A [trcks.oop.AwaitableWrapper][] instance with the original wrapped object.

    Example:
        >>> import asyncio
        >>> from trcks.oop import Wrapper
        >>> async def write_to_disk(s: str) -> None:
        ...     await asyncio.sleep(0.001)
        ...     print(f"Wrote '{s}' to disk.")
        ...
        >>> awaitable_wrapper = Wrapper.construct(
        ...     "Hello, world!"
        ... ).tap_to_awaitable(write_to_disk)
        >>> awaitable_wrapper
        AwaitableWrapper(core=<coroutine object ...>)
        >>> value = asyncio.run(awaitable_wrapper.core_as_coroutine)
        Wrote 'Hello, world!' to disk.
        >>> value
        'Hello, world!'
    """
    return AwaitableWrapper.construct(self.core).tap_to_awaitable(f)

tap_to_awaitable_result(f)

Apply an asynchronous side effect with return type trcks.Result to the wrapped object.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import Result
>>> from trcks.oop import Wrapper
>>> async def write_to_disk(s: str, path: str) -> Result[str, None]:
...     if path != "output.txt":
...         return "failure", "write error"
...     await asyncio.sleep(0.001)
...     print(f"Wrote '{s}' to file {path}.")
...     return "success", None
...
>>> awaitable_result_wrapper_1 = Wrapper.construct(
...     "Hello, world!"
... ).tap_to_awaitable_result(
...     lambda s: write_to_disk(s, "destination.txt")
... )
>>> awaitable_result_wrapper_1
AwaitableResultWrapper(core=<coroutine object ...>)
>>> result_1 = asyncio.run(awaitable_result_wrapper_1.core_as_coroutine)
>>> result_1
('failure', 'write error')
>>> awaitable_result_wrapper_2 = Wrapper.construct(
...     "Hello, world!"
... ).tap_to_awaitable_result(
...     lambda s: write_to_disk(s, "output.txt")
... )
>>> awaitable_result_wrapper_2
AwaitableResultWrapper(core=<coroutine object ...>)
>>> result_2 = asyncio.run(awaitable_result_wrapper_2.core_as_coroutine)
Wrote 'Hello, world!' to file output.txt.
>>> result_2
('success', 'Hello, world!')
Source code in src/trcks/oop.py
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
def tap_to_awaitable_result(
    self, f: Callable[[_T_co], AwaitableResult[_F, object]]
) -> AwaitableResultWrapper[_F, _T_co]:
    """Apply an asynchronous side effect with return type [trcks.Result][]
    to the wrapped object.

    Args:
        f: The asynchronous side effect to be applied.

    Returns:
        A [trcks.oop.AwaitableResultWrapper][] instance with

            - *the returned* [trcks.Failure][]
                if the given side effect returns a [trcks.Failure][] or
            - a [trcks.Success][] instance containing *the original* wrapped object
                if the given side effect returns a [trcks.Success][].

    Example:
        >>> import asyncio
        >>> from trcks import Result
        >>> from trcks.oop import Wrapper
        >>> async def write_to_disk(s: str, path: str) -> Result[str, None]:
        ...     if path != "output.txt":
        ...         return "failure", "write error"
        ...     await asyncio.sleep(0.001)
        ...     print(f"Wrote '{s}' to file {path}.")
        ...     return "success", None
        ...
        >>> awaitable_result_wrapper_1 = Wrapper.construct(
        ...     "Hello, world!"
        ... ).tap_to_awaitable_result(
        ...     lambda s: write_to_disk(s, "destination.txt")
        ... )
        >>> awaitable_result_wrapper_1
        AwaitableResultWrapper(core=<coroutine object ...>)
        >>> result_1 = asyncio.run(awaitable_result_wrapper_1.core_as_coroutine)
        >>> result_1
        ('failure', 'write error')
        >>> awaitable_result_wrapper_2 = Wrapper.construct(
        ...     "Hello, world!"
        ... ).tap_to_awaitable_result(
        ...     lambda s: write_to_disk(s, "output.txt")
        ... )
        >>> awaitable_result_wrapper_2
        AwaitableResultWrapper(core=<coroutine object ...>)
        >>> result_2 = asyncio.run(awaitable_result_wrapper_2.core_as_coroutine)
        Wrote 'Hello, world!' to file output.txt.
        >>> result_2
        ('success', 'Hello, world!')
    """
    return AwaitableResultWrapper.construct_success(
        self.core
    ).tap_success_to_awaitable_result(f)

tap_to_result(f)

Apply a synchronous side effect with return type trcks.Result to the wrapped object.

Parameters:

Returns:

Example
>>> from trcks import Result
>>> from trcks.oop import Wrapper
>>> def print_positive_float(x: float) -> Result[str, None]:
...     if x <= 0:
...         return "failure", "not positive"
...     return "success", print(f"Positive float: {x}")
...
>>> result_wrapper_1 = Wrapper.construct(-2.3).tap_to_result(
...     print_positive_float
... )
>>> result_wrapper_1
ResultWrapper(core=('failure', 'not positive'))
>>> result_wrapper_2 = Wrapper.construct(3.5).tap_to_result(
...     print_positive_float
... )
Positive float: 3.5
>>> result_wrapper_2
ResultWrapper(core=('success', 3.5))
Source code in src/trcks/oop.py
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
def tap_to_result(
    self, f: Callable[[_T_co], Result[_F, object]]
) -> ResultWrapper[_F, _T_co]:
    """Apply a synchronous side effect with return type [trcks.Result][]
    to the wrapped object.

    Args:
        f: The synchronous side effect to be applied.

    Returns:
        A [trcks.oop.ResultWrapper][] instance with

            - *the returned* [trcks.Failure][]
                if the given side effect returns a [trcks.Failure][] or
            - a [trcks.Success][] instance containing *the original* wrapped object
                if the given side effect returns a [trcks.Success][].

    Example:
        >>> from trcks import Result
        >>> from trcks.oop import Wrapper
        >>> def print_positive_float(x: float) -> Result[str, None]:
        ...     if x <= 0:
        ...         return "failure", "not positive"
        ...     return "success", print(f"Positive float: {x}")
        ...
        >>> result_wrapper_1 = Wrapper.construct(-2.3).tap_to_result(
        ...     print_positive_float
        ... )
        >>> result_wrapper_1
        ResultWrapper(core=('failure', 'not positive'))
        >>> result_wrapper_2 = Wrapper.construct(3.5).tap_to_result(
        ...     print_positive_float
        ... )
        Positive float: 3.5
        >>> result_wrapper_2
        ResultWrapper(core=('success', 3.5))
    """
    return ResultWrapper.construct_success(self.core).tap_success_to_result(f)