@@ -412,6 +412,26 @@ def range(start: int, stop: int, step: int) -> Block[int]:
412412 def range (* args : int , ** kw : int ) -> Block [int ]:
413413 return range (* args , ** kw )
414414
415+ def reduce (
416+ self ,
417+ reduction : Callable [[_TSource , _TSource ], _TSource ],
418+ ) -> _TSource :
419+ """Apply a function to each element of the collection, threading an
420+ accumulator argument through the computation. Apply the function to
421+ the first two elements of the list. Then feed this result into the
422+ function along with the third element and so on. Return the final
423+ result. If the input function is f and the elements are i0...iN then
424+ computes f (... (f i0 i1) i2 ...) iN.
425+
426+ Args:
427+ reduction: The function to reduce two list elements to a
428+ single element.
429+
430+ Returns:
431+ Returns the final state value.
432+ """
433+ return reduce (reduction )(self )
434+
415435 @staticmethod
416436 def singleton (item : _TSource ) -> Block [_TSource ]:
417437 return singleton (item )
@@ -646,9 +666,10 @@ def cons(head: _TSource, tail: Block[_TSource]) -> Block[_TSource]:
646666"""The empty list."""
647667
648668
669+ @curry_flipped (1 )
649670def filter (
650- predicate : Callable [[_TSource ], bool ]
651- ) -> Callable [[ Block [_TSource ]], Block [ _TSource ] ]:
671+ source : Block [ _TSource ], predicate : Callable [[_TSource ], bool ]
672+ ) -> Block [_TSource ]:
652673 """Returns a new collection containing only the elements of the
653674 collection for which the given predicate returns `True`
654675
@@ -659,20 +680,7 @@ def filter(
659680 Partially applied filter function.
660681 """
661682
662- def _filter (source : Block [_TSource ]) -> Block [_TSource ]:
663- """Returns a new collection containing only the elements of the
664- collection for which the given predicate returns `True`
665-
666- Args:
667- source: The input list.
668-
669- Returns:
670- A list containing only the elements that satisfy the
671- predicate.
672- """
673- return source .filter (predicate )
674-
675- return _filter
683+ return source .filter (predicate )
676684
677685
678686def fold (
@@ -788,6 +796,34 @@ def _map(source: Block[_TSource]) -> Block[_TResult]:
788796 return _map
789797
790798
799+ @curry_flipped (1 )
800+ def reduce (
801+ source : Block [_TSource ],
802+ reduction : Callable [[_TSource , _TSource ], _TSource ],
803+ ) -> _TSource :
804+ """Apply a function to each element of the collection, threading an
805+ accumulator argument through the computation. Apply the function to
806+ the first two elements of the list. Then feed this result into the
807+ function along with the third element and so on. Return the final
808+ result. If the input function is f and the elements are i0...iN then
809+ computes f (... (f i0 i1) i2 ...) iN.
810+
811+ Args:
812+ source: The input block (curried flipped)
813+ reduction: The function to reduce two list elements to a single
814+ element.
815+
816+ Returns:
817+ Partially applied reduce function that takes the source block
818+ and returns the final state value.
819+ """
820+
821+ if source .is_empty ():
822+ raise ValueError ("Collection was empty" )
823+
824+ return source .tail ().fold (reduction , source .head ())
825+
826+
791827@overload
792828def starmap (
793829 mapper : Callable [[_T1 , _T2 ], _TResult ]
0 commit comments