Recap - Pipeline Builder
Part of the Logic & Flow section of Coddy's Swift journey — lesson 43 of 56.
Challenge
MediumBuild a small data pipeline driven by closures.
Read three lines:
- An integer
min - An integer
scale - A comma-separated list of integers
Define two closure factories:
makeFilter(min: Int) -> (Int) -> Boolcapturesminand returns a predicaten > minmakeScaler(by: Int) -> (Int) -> Intcapturesbyand 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:
- The numbers that survived the filter, joined with
, - The same numbers after scaling, joined with
, - The sum of the scaled values
For input 5 / 3 / 2,7,3,8,9, the output is:
7,8,9
21,24,27
72Try 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
1Strings In Depth
Count and IndicesCase and TrimSearching in StringsSplitting and JoiningReplacing SubstringsRecap - Username Check8Closures
Closure BasicsTrailing ClosuresCapturing ValuesReturning ClosuresCustom Higher-OrderRecap - Pipeline Builder