Menu
Coddy logo textTech

Recap - Pipeline Builder

Part of the Logic & Flow section of Coddy's Swift journey — lesson 43 of 56.

challenge icon

Challenge

Medium

Build a small data pipeline driven by closures.

Read three lines:

  1. An integer min
  2. An integer scale
  3. A comma-separated list of integers

Define two closure factories:

  • makeFilter(min: Int) -> (Int) -> Bool captures min and returns a predicate n > min
  • makeScaler(by: Int) -> (Int) -> Int captures by and returns a function that multiplies by it

Then define a function pipeline(_ nums: [Int], filter: (Int) -> Bool, transform: (Int) -> Int) -> [Int] that filters first, then transforms.

Print three lines:

  1. The numbers that survived the filter, joined with ,
  2. The same numbers after scaling, joined with ,
  3. The sum of the scaled values

For input 5 / 3 / 2,7,3,8,9, the output is:

7,8,9
21,24,27
72

Try it yourself

let minVal = Int(readLine()!)!
let scale = Int(readLine()!)!
let nums = readLine()!.components(separatedBy: ",").map { Int($0)! }

// TODO: makeFilter / makeScaler returning closures; pipeline(_:filter:transform:);
// then print filtered, scaled, and the sum of scaled

All lessons in Logic & Flow