ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

F# Pipeline Workflow: Data Transformation Pipeline

F#

Goal -- WPM

Ready
Exercise Algorithm Area
1(*@
2Filters a list of integers, keeping only the odd numbers.
3*@)
4let filterOddNumbers (numbers: list<int>) : list<int> =
5numbers |> List.filter (fun n -> n % 2 <> 0)
6
7(*@
8Squares each integer in a list.
9*@)
10let squareNumbers (numbers: list<int>) : list<int> =
11numbers |> List.map (fun n -> n * n)
12
13(*@
14Sums all integers in a list.
15*@)
16let sumNumbers (numbers: list<int>) : int =
17numbers |> List.sum
18
19(*@
20A function to create an empty list of integers.
21*@)
22let createEmptyIntList () : list<int> =
23[]
24
25(*@
26A function to create a list of integers with some values.
27*@)
28let createSampleIntList () : list<int> =
29[1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
30
31(*@
32The main data transformation pipeline.
33It takes a list of integers and applies a sequence of transformations.
341. Filter out even numbers.
352. Square the remaining odd numbers.
363. Sum the squared odd numbers.
37*@)
38let processNumberPipeline (inputList: list<int>) : int =
39// Handle empty list edge case explicitly
40if List.isEmpty inputList then
410
42else
43inputList
44|> filterOddNumbers
45|> squareNumbers
46|> sumNumbers
47
48(*@
49A slightly different pipeline that also includes a check for negative numbers.
50This is for demonstration of adding more steps.
51*@)
52let processNumberPipelineWithNegativeCheck (inputList: list<int>) : int =
53if List.isEmpty inputList then
540
55else
56inputList
57|> List.filter (fun n -> n > 0) // Filter out non-positive numbers
58|> filterOddNumbers
59|> squareNumbers
60|> sumNumbers
61
62(*@
63Another helper function to create a list with negative numbers.
64*@)
65let createListWithNegatives () : list<int> =
66[-1; -2; 3; 4; -5; 6]
67
68(*@
69A pipeline that returns the list of squared odd numbers, not the sum.
70*@)
71let getSquaredOddNumbers (inputList: list<int>) : list<int> =
72if List.isEmpty inputList then
73[]
74else
75inputList
76|> filterOddNumbers
77|> squareNumbers
78
79(*@
80A pipeline that only filters and squares.
81*@)
82let filterAndSquare (inputList: list<int>) : list<int> =
83inputList
84|> filterOddNumbers
85|> squareNumbers
86
87(*@
88A pipeline that only filters and sums.
89*@)
90let filterAndSum (inputList: list<int>) : list<int> =
91inputList
92|> filterOddNumbers
93|> sumNumbers
94
95(*@
96The final pipeline implementation.
97*@)
98let finalPipeline (inputList: list<int>) : int =
99inputList
100|> filterOddNumbers
101|> squareNumbers
102|> sumNumbers
Algorithm description viewbox

F# Pipeline Workflow: Data Transformation Pipeline

Algorithm description:

This F# code demonstrates a data transformation pipeline using the pipeline operator `|>`. It defines a sequence of functions that process a list of integers: first filtering for odd numbers, then squaring them, and finally summing the results. This approach emphasizes a clear, step-by-step data flow, making the transformations easy to read and understand. It also includes handling for an empty input list, ensuring robustness.

Algorithm explanation:

The `processNumberPipeline` function exemplifies a functional data pipeline. It takes an `inputList` and applies a series of transformations sequentially using the `|>` operator. `filterOddNumbers` uses `List.filter` to keep only elements `n` where `n % 2 <> 0`. `squareNumbers` uses `List.map` to apply the squaring function `n * n` to each element. Finally, `sumNumbers` uses `List.sum` to aggregate the results. The time complexity is O(N) where N is the number of elements in the input list, as each element is processed by each stage of the pipeline. The space complexity is O(N) in the worst case, as intermediate lists are created (though F# can optimize some of this). An edge case handled is an empty input list, which correctly returns 0. The correctness relies on the well-defined behavior of `List.filter`, `List.map`, and `List.sum`, and the sequential application ensures the intended order of operations.

Pseudocode:

function filterOddNumbers(numbers):
  return numbers filtered to keep only odd numbers

function squareNumbers(numbers):
  return each number in numbers squared

function sumNumbers(numbers):
  return the sum of all numbers in the list

function processNumberPipeline(inputList):
  if inputList is empty:
    return 0
  else:
    result = inputList |> filterOddNumbers
    result = result |> squareNumbers
    result = result |> sumNumbers
    return result