Hone logo
Hone
Problems

Building a Data Processing Pipeline in Go

The pipeline pattern is a powerful design pattern for processing data sequentially. It involves chaining functions together, where the output of one function becomes the input of the next. This challenge asks you to implement a basic data processing pipeline in Go, demonstrating how to chain functions to transform data.

Problem Description

You are tasked with creating a pipeline that processes a slice of integers. The pipeline should consist of three stages:

  1. Square: Each integer in the input slice should be squared.
  2. Filter: Only even numbers from the squared values should be kept.
  3. Sum: The remaining even numbers should be summed together.

Your solution should define three separate functions, one for each stage of the pipeline. These functions should accept a slice of integers as input and return a slice of integers as output (except for the final Sum function, which should return an integer). The pipeline should be constructed by chaining these functions together.

Key Requirements:

  • Implement three distinct functions: Square, Filter, and Sum.
  • Each function should operate on a slice of integers.
  • Square should return the square of each number.
  • Filter should return only the even numbers from the input slice.
  • Sum should return the sum of all numbers in the input slice.
  • The pipeline should be constructed by passing the output of one function as the input to the next.
  • The final result should be the sum of the even squares of the original input numbers.

Expected Behavior:

The program should take a slice of integers as input, process it through the pipeline, and print the final sum.

Edge Cases to Consider:

  • Empty input slice: The sum should be 0.
  • Slice containing only odd numbers: The sum should be 0.
  • Large input slices: Consider potential performance implications (though optimization is not the primary focus of this challenge).

Examples

Example 1:

Input: [1, 2, 3, 4, 5]
Output: 42
Explanation:
1. Square: [1, 4, 9, 16, 25]
2. Filter: [4, 16]
3. Sum: 4 + 16 = 20

Example 2:

Input: [2, 4, 6]
Output: 56
Explanation:
1. Square: [4, 16, 36]
2. Filter: [4, 16, 36]
3. Sum: 4 + 16 + 36 = 56

Example 3:

Input: [1, 3, 5, 7]
Output: 0
Explanation:
1. Square: [1, 9, 25, 49]
2. Filter: []
3. Sum: 0

Constraints

  • The input slice will contain only non-negative integers.
  • The length of the input slice can be up to 1000.
  • The integers in the input slice will be less than or equal to 100.
  • Performance is not a primary concern, but avoid unnecessarily inefficient solutions.

Notes

  • Consider using anonymous functions or closures to create a more concise pipeline.
  • The pipeline pattern promotes code reusability and modularity.
  • Think about how to make your code readable and maintainable.
  • The Sum function should return an int, not a slice.
  • Focus on the core concept of chaining functions together to process data sequentially.
Loading editor...
go