sirenbyte commented on code in PR #23577:
URL: https://github.com/apache/beam/pull/23577#discussion_r1116007372


##########
learning/tour-of-beam/learning-content/common-transforms/filter/description.md:
##########
@@ -0,0 +1,421 @@
+<!--
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+http://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+### Using Filter
+
+PCollection datasets can be filtered using the Filter transform. You can 
create a filter by supplying a predicate and, when applied, filtering out all 
the elements of PCollection that don’t satisfy the predicate.
+
+{{if (eq .Sdk "go")}}
+```
+import (
+  "github.com/apache/fbeam/sdks/go/pkg/beam"
+  "github.com/apache/beam/sdks/go/pkg/beam/transforms/filter"
+)
+
+func ApplyTransform(s beam.Scope, input beam.PCollection) beam.PCollection {
+  return filter.Exclude(s, input, func(element int) bool {
+    return element % 2 == 1
+  })
+}
+```
+{{end}}
+{{if (eq .Sdk "java")}}
+```
+PCollection<String> allStrings = pipeline
+        .apply(Create.of(List.of("Hello","world","Hi")));
+
+PCollection<String> filteredStrings = allStrings
+        .apply(Filter.by(new SerializableFunction<String, Boolean>() {
+            @Override
+            public Boolean apply(String input) {
+                return input.length() > 3;
+            }
+        }));
+```
+
+Output
+
+```
+Hello
+world
+```
+
+### Built-in filters
+
+The Java SDK has several filter methods built-in, like Filter.greaterThan and 
Filter.lessThen.  With Filter.greaterThan, the input PCollection can be 
filtered so that only the elements whose values are greater than the specified 
amount remain. Similarly, you can use Filter.lessThen to filter out elements of 
the input PCollection whose values are greater than the specified amount.
+
+Other built-in filters are:
+
+* Filter.greaterThanEq
+* Filter.greaterThan
+* Filter.lessThan
+* Filter.lessThanEq
+* Filter.equal
+
+
+## Example 2: Filtering with a built-in methods
+
+```
+// List of integers
+PCollection<Integer> numbers = pipeline.apply(Create.of(List.of(1, 2, 3, 4, 5, 
6, 7, 8, 9, 10)));
+
+// PCollection will contain [3, 4, 5, 6, 7, 8, 9, 10] at this point

Review Comment:
   Done



##########
learning/tour-of-beam/learning-content/common-transforms/filter/description.md:
##########
@@ -0,0 +1,421 @@
+<!--
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+http://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+### Using Filter
+
+PCollection datasets can be filtered using the Filter transform. You can 
create a filter by supplying a predicate and, when applied, filtering out all 
the elements of PCollection that don’t satisfy the predicate.
+
+{{if (eq .Sdk "go")}}
+```
+import (
+  "github.com/apache/fbeam/sdks/go/pkg/beam"
+  "github.com/apache/beam/sdks/go/pkg/beam/transforms/filter"
+)
+
+func ApplyTransform(s beam.Scope, input beam.PCollection) beam.PCollection {
+  return filter.Exclude(s, input, func(element int) bool {
+    return element % 2 == 1
+  })
+}
+```
+{{end}}
+{{if (eq .Sdk "java")}}
+```
+PCollection<String> allStrings = pipeline
+        .apply(Create.of(List.of("Hello","world","Hi")));
+
+PCollection<String> filteredStrings = allStrings
+        .apply(Filter.by(new SerializableFunction<String, Boolean>() {
+            @Override
+            public Boolean apply(String input) {
+                return input.length() > 3;
+            }
+        }));
+```
+
+Output
+
+```
+Hello
+world
+```
+
+### Built-in filters
+
+The Java SDK has several filter methods built-in, like Filter.greaterThan and 
Filter.lessThen.  With Filter.greaterThan, the input PCollection can be 
filtered so that only the elements whose values are greater than the specified 
amount remain. Similarly, you can use Filter.lessThen to filter out elements of 
the input PCollection whose values are greater than the specified amount.
+
+Other built-in filters are:
+
+* Filter.greaterThanEq
+* Filter.greaterThan
+* Filter.lessThan
+* Filter.lessThanEq
+* Filter.equal
+
+
+## Example 2: Filtering with a built-in methods
+
+```
+// List of integers
+PCollection<Integer> numbers = pipeline.apply(Create.of(List.of(1, 2, 3, 4, 5, 
6, 7, 8, 9, 10)));
+
+// PCollection will contain [3, 4, 5, 6, 7, 8, 9, 10] at this point
+PCollection<Integer> greaterThanEqNumbers = 
pipeline.apply(Filter.greaterThanEq(3));
+
+
+// PCollection will contain [5, 6, 7, 8, 9, 10] at this point
+PCollection<Integer> greaterThanNumbers = 
pipeline.apply(Filter.greaterThan(4));
+
+
+// PCollection will contain [1, 2, 3, 4, 5, 6, 7, 8, 9] at this point
+PCollection<Integer> lessThanNumbers = pipeline.apply(Filter.lessThan(10));
+
+
+// PCollection will contain [1, 2, 3, 4 5, 6, 7] at this point
+PCollection<Integer> lessThanEqNumbers = pipeline.apply(Filter.lessThanEq(7));
+
+
+// PCollection will contain [9] at this point
+PCollection<Integer> equalNumbers = pipeline.apply(Filter.equal(9));
+```
+{{end}}
+{{if (eq .Sdk "python")}}
+```
+import apache_beam as beam
+
+from log_elements import LogElements
+
+with beam.Pipeline() as p:
+  (p | beam.Create(range(1, 11))
+     | beam.Filter(lambda num: num % 2 == 0)
+     | LogElements())
+```
+
+
+### Example 1: Filtering with a function
+
+You can define a function `is_perennial()` which returns True if the element’s 
duration equals 'perennial', and False otherwise.
+
+```
+import apache_beam as beam
+
+def is_perennial(plant):
+  return plant['duration'] == 'perennial'
+
+with beam.Pipeline() as pipeline:
+  perennials = (
+      pipeline

Review Comment:
   Changed in all modules



##########
learning/tour-of-beam/learning-content/common-transforms/filter/description.md:
##########
@@ -0,0 +1,421 @@
+<!--
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+http://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+### Using Filter
+
+PCollection datasets can be filtered using the Filter transform. You can 
create a filter by supplying a predicate and, when applied, filtering out all 
the elements of PCollection that don’t satisfy the predicate.
+
+{{if (eq .Sdk "go")}}
+```
+import (
+  "github.com/apache/fbeam/sdks/go/pkg/beam"
+  "github.com/apache/beam/sdks/go/pkg/beam/transforms/filter"
+)
+
+func ApplyTransform(s beam.Scope, input beam.PCollection) beam.PCollection {
+  return filter.Exclude(s, input, func(element int) bool {
+    return element % 2 == 1
+  })
+}
+```
+{{end}}
+{{if (eq .Sdk "java")}}
+```
+PCollection<String> allStrings = pipeline
+        .apply(Create.of(List.of("Hello","world","Hi")));
+
+PCollection<String> filteredStrings = allStrings
+        .apply(Filter.by(new SerializableFunction<String, Boolean>() {
+            @Override
+            public Boolean apply(String input) {
+                return input.length() > 3;
+            }
+        }));
+```
+
+Output
+
+```
+Hello
+world
+```
+
+### Built-in filters
+
+The Java SDK has several filter methods built-in, like Filter.greaterThan and 
Filter.lessThen.  With Filter.greaterThan, the input PCollection can be 
filtered so that only the elements whose values are greater than the specified 
amount remain. Similarly, you can use Filter.lessThen to filter out elements of 
the input PCollection whose values are greater than the specified amount.
+
+Other built-in filters are:
+
+* Filter.greaterThanEq
+* Filter.greaterThan
+* Filter.lessThan
+* Filter.lessThanEq
+* Filter.equal
+
+
+## Example 2: Filtering with a built-in methods
+
+```
+// List of integers
+PCollection<Integer> numbers = pipeline.apply(Create.of(List.of(1, 2, 3, 4, 5, 
6, 7, 8, 9, 10)));
+
+// PCollection will contain [3, 4, 5, 6, 7, 8, 9, 10] at this point
+PCollection<Integer> greaterThanEqNumbers = 
pipeline.apply(Filter.greaterThanEq(3));
+
+
+// PCollection will contain [5, 6, 7, 8, 9, 10] at this point
+PCollection<Integer> greaterThanNumbers = 
pipeline.apply(Filter.greaterThan(4));
+
+
+// PCollection will contain [1, 2, 3, 4, 5, 6, 7, 8, 9] at this point
+PCollection<Integer> lessThanNumbers = pipeline.apply(Filter.lessThan(10));
+
+
+// PCollection will contain [1, 2, 3, 4 5, 6, 7] at this point
+PCollection<Integer> lessThanEqNumbers = pipeline.apply(Filter.lessThanEq(7));
+
+
+// PCollection will contain [9] at this point
+PCollection<Integer> equalNumbers = pipeline.apply(Filter.equal(9));
+```
+{{end}}
+{{if (eq .Sdk "python")}}
+```
+import apache_beam as beam
+
+from log_elements import LogElements
+
+with beam.Pipeline() as p:
+  (p | beam.Create(range(1, 11))
+     | beam.Filter(lambda num: num % 2 == 0)
+     | LogElements())
+```
+
+
+### Example 1: Filtering with a function
+
+You can define a function `is_perennial()` which returns True if the element’s 
duration equals 'perennial', and False otherwise.
+
+```
+import apache_beam as beam
+
+def is_perennial(plant):
+  return plant['duration'] == 'perennial'
+
+with beam.Pipeline() as pipeline:
+  perennials = (
+      pipeline
+      | 'Gardening plants' >> beam.Create([
+          {
+              'icon': '🍓', 'name': 'Strawberry', 'duration': 'perennial'
+          },
+          {
+              'icon': '🥕', 'name': 'Carrot', 'duration': 'biennial'
+          },
+          {
+              'icon': '🍆', 'name': 'Eggplant', 'duration': 'perennial'
+          },
+          {
+              'icon': '🍅', 'name': 'Tomato', 'duration': 'annual'
+          },
+          {
+              'icon': '🥔', 'name': 'Potato', 'duration': 'perennial'
+          },
+      ])
+      | 'Filter perennials' >> beam.Filter(is_perennial)
+      | beam.Map(print))
+```
+
+Output
+
+```
+{'icon': '🍓', 'name': 'Strawberry', 'duration': 'perennial'}
+{'icon': '🍆', 'name': 'Eggplant', 'duration': 'perennial'}
+{'icon': '🥔', 'name': 'Potato', 'duration': 'perennial'}
+```
+
+### Example 2: Filtering with a lambda function
+
+You can also use lambda functions to simplify Example 1.
+
+```
+import apache_beam as beam
+
+with beam.Pipeline() as pipeline:
+  perennials = (
+      pipeline

Review Comment:
   Changed in all modules



##########
learning/tour-of-beam/learning-content/common-transforms/filter/description.md:
##########
@@ -0,0 +1,421 @@
+<!--
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+http://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+### Using Filter
+
+PCollection datasets can be filtered using the Filter transform. You can 
create a filter by supplying a predicate and, when applied, filtering out all 
the elements of PCollection that don’t satisfy the predicate.
+
+{{if (eq .Sdk "go")}}
+```
+import (
+  "github.com/apache/fbeam/sdks/go/pkg/beam"
+  "github.com/apache/beam/sdks/go/pkg/beam/transforms/filter"
+)
+
+func ApplyTransform(s beam.Scope, input beam.PCollection) beam.PCollection {
+  return filter.Exclude(s, input, func(element int) bool {
+    return element % 2 == 1
+  })
+}
+```
+{{end}}
+{{if (eq .Sdk "java")}}
+```
+PCollection<String> allStrings = pipeline
+        .apply(Create.of(List.of("Hello","world","Hi")));
+
+PCollection<String> filteredStrings = allStrings
+        .apply(Filter.by(new SerializableFunction<String, Boolean>() {
+            @Override
+            public Boolean apply(String input) {
+                return input.length() > 3;
+            }
+        }));
+```
+
+Output
+
+```
+Hello
+world
+```
+
+### Built-in filters
+
+The Java SDK has several filter methods built-in, like Filter.greaterThan and 
Filter.lessThen.  With Filter.greaterThan, the input PCollection can be 
filtered so that only the elements whose values are greater than the specified 
amount remain. Similarly, you can use Filter.lessThen to filter out elements of 
the input PCollection whose values are greater than the specified amount.
+
+Other built-in filters are:
+
+* Filter.greaterThanEq
+* Filter.greaterThan
+* Filter.lessThan
+* Filter.lessThanEq
+* Filter.equal
+
+
+## Example 2: Filtering with a built-in methods
+
+```
+// List of integers
+PCollection<Integer> numbers = pipeline.apply(Create.of(List.of(1, 2, 3, 4, 5, 
6, 7, 8, 9, 10)));
+
+// PCollection will contain [3, 4, 5, 6, 7, 8, 9, 10] at this point
+PCollection<Integer> greaterThanEqNumbers = 
pipeline.apply(Filter.greaterThanEq(3));
+
+
+// PCollection will contain [5, 6, 7, 8, 9, 10] at this point
+PCollection<Integer> greaterThanNumbers = 
pipeline.apply(Filter.greaterThan(4));
+
+
+// PCollection will contain [1, 2, 3, 4, 5, 6, 7, 8, 9] at this point
+PCollection<Integer> lessThanNumbers = pipeline.apply(Filter.lessThan(10));
+
+
+// PCollection will contain [1, 2, 3, 4 5, 6, 7] at this point
+PCollection<Integer> lessThanEqNumbers = pipeline.apply(Filter.lessThanEq(7));
+
+
+// PCollection will contain [9] at this point
+PCollection<Integer> equalNumbers = pipeline.apply(Filter.equal(9));
+```
+{{end}}
+{{if (eq .Sdk "python")}}
+```
+import apache_beam as beam
+
+from log_elements import LogElements
+
+with beam.Pipeline() as p:
+  (p | beam.Create(range(1, 11))
+     | beam.Filter(lambda num: num % 2 == 0)
+     | LogElements())
+```
+
+
+### Example 1: Filtering with a function
+
+You can define a function `is_perennial()` which returns True if the element’s 
duration equals 'perennial', and False otherwise.
+
+```
+import apache_beam as beam
+
+def is_perennial(plant):
+  return plant['duration'] == 'perennial'
+
+with beam.Pipeline() as pipeline:
+  perennials = (
+      pipeline
+      | 'Gardening plants' >> beam.Create([
+          {
+              'icon': '🍓', 'name': 'Strawberry', 'duration': 'perennial'
+          },
+          {
+              'icon': '🥕', 'name': 'Carrot', 'duration': 'biennial'
+          },
+          {
+              'icon': '🍆', 'name': 'Eggplant', 'duration': 'perennial'
+          },
+          {
+              'icon': '🍅', 'name': 'Tomato', 'duration': 'annual'
+          },
+          {
+              'icon': '🥔', 'name': 'Potato', 'duration': 'perennial'
+          },
+      ])
+      | 'Filter perennials' >> beam.Filter(is_perennial)
+      | beam.Map(print))
+```
+
+Output
+
+```
+{'icon': '🍓', 'name': 'Strawberry', 'duration': 'perennial'}
+{'icon': '🍆', 'name': 'Eggplant', 'duration': 'perennial'}
+{'icon': '🥔', 'name': 'Potato', 'duration': 'perennial'}
+```
+
+### Example 2: Filtering with a lambda function
+
+You can also use lambda functions to simplify Example 1.
+
+```
+import apache_beam as beam
+
+with beam.Pipeline() as pipeline:
+  perennials = (
+      pipeline
+      | 'Gardening plants' >> beam.Create([
+          {
+              'icon': '🍓', 'name': 'Strawberry', 'duration': 'perennial'
+          },
+          {
+              'icon': '🥕', 'name': 'Carrot', 'duration': 'biennial'
+          },
+          {
+              'icon': '🍆', 'name': 'Eggplant', 'duration': 'perennial'
+          },
+          {
+              'icon': '🍅', 'name': 'Tomato', 'duration': 'annual'
+          },
+          {
+              'icon': '🥔', 'name': 'Potato', 'duration': 'perennial'
+          },
+      ])
+      | 'Filter perennials' >>
+      beam.Filter(lambda plant: plant['duration'] == 'perennial')
+      | beam.Map(print))
+```
+
+Output
+
+```
+{'icon': '🍓', 'name': 'Strawberry', 'duration': 'perennial'}
+{'icon': '🍆', 'name': 'Eggplant', 'duration': 'perennial'}
+{'icon': '🥔', 'name': 'Potato', 'duration': 'perennial'}
+```
+
+### Example 3: Filtering with multiple arguments
+
+You can pass functions with multiple arguments to ```Filter```. They are 
passed as additional positional arguments or keyword arguments to the function.
+
+In this example, ```has_duration``` takes ```plant``` and ```duration``` as 
arguments.
+
+```
+import apache_beam as beam
+
+def has_duration(plant, duration):
+  return plant['duration'] == duration
+
+with beam.Pipeline() as pipeline:

Review Comment:
   Changed in all modules



-- 
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