|
16 | 16 | # - https://github.com/fsharp/fsharp/blob/master/src/fsharp/FSharp.Core/map.fs |
17 | 17 | from __future__ import annotations |
18 | 18 |
|
19 | | -from collections.abc import Callable, ItemsView, Iterable, Iterator, Mapping |
| 19 | +import asyncio |
| 20 | +from collections.abc import Awaitable, Callable, ItemsView, Iterable, Iterator, Mapping |
20 | 21 | from typing import Any, TypeVar, cast |
21 | 22 |
|
22 | 23 | from expression.core import Option, PipeMixin, SupportsLessThan, curry_flip, pipe |
@@ -114,6 +115,25 @@ def map(self, mapping: Callable[[_Key, _Value], _Result]) -> Map[_Key, _Result]: |
114 | 115 | """ |
115 | 116 | return Map(maptree.map(mapping, self._tree)) |
116 | 117 |
|
| 118 | + async def par_map(self, mapping: Callable[[_Key, _Value], Awaitable[_Result]]) -> Map[_Key, _Result]: |
| 119 | + """Map the mapping asynchronously. |
| 120 | +
|
| 121 | + Builds a new collection whose elements are the results of |
| 122 | + applying the given asynchronous function to each of the elements |
| 123 | + of the collection. The key passed to the function indicates the |
| 124 | + key of element being transformed. |
| 125 | +
|
| 126 | + Args: |
| 127 | + mapping: The function to transform the key/value pairs |
| 128 | +
|
| 129 | + Returns: |
| 130 | + The resulting map of keys and transformed values. |
| 131 | + """ |
| 132 | + keys_and_values = self.to_seq() |
| 133 | + result = await asyncio.gather(*(mapping(key, value) for key, value in keys_and_values)) |
| 134 | + keys = [key for key, _ in keys_and_values] |
| 135 | + return Map.of_seq(zip(keys, result)) |
| 136 | + |
117 | 137 | def partition(self, predicate: Callable[[_Key, _Value], bool]) -> tuple[Map[_Key, _Value], Map[_Key, _Value]]: |
118 | 138 | r1, r2 = maptree.partition(predicate, self._tree) |
119 | 139 | return Map(r1), Map(r2) |
|
0 commit comments