gemini-code-assist[bot] commented on code in PR #37408:
URL: https://github.com/apache/beam/pull/37408#discussion_r2728752247


##########
sdks/typescript/src/apache_beam/pvalue.ts:
##########
@@ -163,6 +163,55 @@ export class PCollection<T> {
     );
   }
 
+  /**
+   * Returns a PCollection containing only elements that satisfy the given
+   * predicate function.
+   *
+   * This is analogous to JavaScript's `Array.filter()` method.
+   *
+   * Example usage:
+   * ```
+   * const evens = pcoll.filter(x => x % 2 === 0);
+   * const positives = pcoll.filter(x => x > 0);
+   * ```
+   *
+   * @param fn A predicate function that returns true for elements to keep,
+   *           false for elements to filter out. The function receives the
+   *           element and optionally a context object.
+   * @param context Optional context object to pass to the predicate function.
+   * @returns A new PCollection containing only the elements for which the
+   *          predicate returned true.
+   */
+  filter<ContextT extends Object | undefined = undefined>(
+    fn:
+      | (ContextT extends undefined ? (element: T) => boolean : never)
+      | ((element: T, context: ContextT) => boolean),
+    context: ContextT = undefined!,
+  ): PCollection<T> {
+    if (extractContext(fn)) {
+      context = { ...extractContext(fn), ...context };
+    }
+    return this.apply(
+      withName(
+        "filter(" + extractName(fn) + ")",
+        parDo<T, T, ContextT>(
+          {
+            process: function (element: T, context: ContextT) {
+              // Return the element wrapped in an array if predicate is true,
+              // otherwise return an empty array to filter it out.
+              const keep =
+                context === null || context === undefined
+                  ? (fn as (element: T) => boolean)(element)
+                  : fn(element, context);
+              return keep ? [element] : [];
+            },

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   For consistency with the `map` implementation and to potentially improve 
performance by avoiding array allocations, consider using a generator function 
for the `process` method. This would involve changing `function` to `function*` 
and using `yield element` instead of returning an array.
   
   ```typescript
               process: function* (element: T, context: ContextT) {
                 // Yield the element if the predicate is true, otherwise do 
nothing to filter it out.
                 const keep =
                   context === null || context === undefined
                     ? (fn as (element: T) => boolean)(element)
                     : fn(element, context);
                 if (keep) {
                   yield element;
                 }
               },
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to