Skip to content

Commit 6f6bc59

Browse files
authored
Fix docstrings for collect (#235)
1 parent efd6d00 commit 6f6bc59

File tree

6 files changed

+358
-277
lines changed

6 files changed

+358
-277
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ repos:
1414
language: node
1515
pass_filenames: false
1616
types: [python]
17-
additional_dependencies: ["[email protected].386"]
17+
additional_dependencies: ["[email protected].391"]
1818
repo: local

expression/collections/block.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ def mapper(x: _TSource) -> Block[_TResult]:
113113
return self.collect(mapper)
114114

115115
def collect(self, mapping: Callable[[_TSource], Block[_TResult]]) -> Block[_TResult]:
116+
"""Collect items from the list.
117+
118+
Applies the given function to each element of the list and concatenates all the
119+
resulting sequences. This function is known as `bind` or `flat_map` in other
120+
languages.
121+
122+
Args:
123+
mapping: The function to generate sequences from the elements.
124+
125+
Returns:
126+
A list comprising the concatenated values from the mapping
127+
function.
128+
"""
116129
mapped = builtins.map(mapping, self._value)
117130
xs = (y for x in mapped for y in x)
118131
return Block(xs)
@@ -556,19 +569,19 @@ def choose(source: Block[_TSource], chooser: Callable[[_TSource], Option[_TResul
556569

557570
@curry_flip(1)
558571
def collect(source: Block[_TSource], mapping: Callable[[_TSource], Block[_TResult]]) -> Block[_TResult]:
559-
"""Collect block.
572+
"""Collect items from the list.
560573
561-
For each element of the list, applies the given function.
562-
Concatenates all the results and return the combined list.
574+
Applies the given function to each element of the list and
575+
concatenates all the resulting sequences. This function is known as
576+
`bind` or `flat_map` in other languages.
563577
564578
Args:
565579
source: The input list (curried flipped).
566-
mapping: The function to transform each input element into
567-
a sublist to be concatenated.
580+
mapping: The function to generate sequences from the elements.
568581
569582
Returns:
570-
A partially applied collect function that takes the source
571-
list and returns the concatenation of the transformed sublists.
583+
A sequence comprising the concatenated values from the mapping
584+
function.
572585
"""
573586
return source.collect(mapping)
574587

expression/collections/seq.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ def choose(self, chooser: Callable[[_TSource], Option[_TResult]]) -> Seq[_TResul
110110
return Seq(xs)
111111

112112
def collect(self, mapping: Callable[[_TSource], Seq[_TResult]]) -> Seq[_TResult]:
113+
"""Collect items from the sequence.
114+
115+
Applies the given function to each element of the list and
116+
concatenates all the resulting sequences. This function is known
117+
as `bind` or `flat_map` in other languages.
118+
119+
Args:
120+
mapping: The function to generate sequences from the elements.
121+
122+
Returns:
123+
A sequence comprising the concatenated values from the mapping
124+
function.
125+
"""
113126
xs = pipe(self, collect(mapping))
114127
return Seq(xs)
115128

@@ -429,6 +442,21 @@ def collect(
429442
source: Iterable[_TSource],
430443
mapping: Callable[[_TSource], Iterable[_TResult]],
431444
) -> Iterable[_TResult]:
445+
"""Collect items from the sequence.
446+
447+
Applies the given function to each element of the list and
448+
concatenates all the resulting sequences. This function is known as
449+
`bind` or `flat_map` in other languages.
450+
451+
Args:
452+
source: The input sequence to to collect from.
453+
mapping: The function to generate sequences from the elements.
454+
455+
Returns:
456+
A sequence comprising the concatenated values from the mapping
457+
function.
458+
"""
459+
432460
def gen() -> Iterator[_TResult]:
433461
for xs in source:
434462
yield from mapping(xs)

expression/core/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def delay(self, fn: Callable[[], _TOuter]) -> _TOuter:
4242
return fn()
4343

4444
def run(self, xs: _TOuter) -> _TOuter:
45-
"""Default implementation assumes the result is already evalutated."""
45+
"""Default implementation assumes the result is already evaluated."""
4646
return xs
4747

4848
def _send(

0 commit comments

Comments
 (0)