Swift Academy · Lesson

Function Composition and Pipelines

Building data pipelines by composing small functions into larger transformations.

Lesson 3 of 412 steps

Function Composition and Pipelines is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

Function composition combines small, focused functions into larger transformations. It's the essence of functional programming — build complex behaviour from simple parts.

Manual Composition

```swift func double(_ n: Int) -> Int { n * 2 } func addOne(_ n: Int) -> Int { n + 1 } let result = addOne(double(5)) // 11 ```

Composing with a Helper

```swift func compose(_ f: @escaping (B)->C, _ g: @escaping (A)->B) -> (A)->C { { f(g($0)) } } let doubleAndAdd = compose(addOne, double) print(doubleAndAdd(5)) // 11 ```

Custom >>> Operator

```swift infixop precedencegroup CompositionPrecedence { ... } func >>> (f: @escaping (A)->B, g: @escaping (B)->C) -> (A)->C { { g(f($0)) } } let pipeline = double >>> addOne >>> String.init print(pipeline(5)) // "11" ```

Pipeline with map/filter Chain

```swift let process: ([Int]) -> [String] = { nums in nums.filter { $0 > 3 }.map { String($0 * 2) } } print(process([1,2,3,4,5])) // ["8","10"] ```

Point-Free Style

Point-free avoids naming intermediate arguments: ```swift let words = ["hello","WORLD","Swift"] let lower = words.map { $0.lowercased() } // named let lower2 = words.map(\.description) // point-free where applicable ```

KeyPaths as Functions

Swift key paths can be used as functions: ```swift struct User { var name: String } let users = [User(name:"Alice"),User(name:"Bob")] let names = users.map(\.name) // ["Alice","Bob"] ```

Building a Data Pipeline

```swift let pipeline: (String) -> Int? = { s in let trimmed = s.trimmingCharacters(in:.whitespaces) guard !trimmed.isEmpty else { return nil } return Int(trimmed) } [" 42 "," ","7"].compactMap(pipeline) // [42,7] ```

Partial Application

```swift func add(_ a: Int) -> (Int) -> Int { { a + $0 } } let add5 = add(5) print([1,2,3].map(add5)) // [6,7,8] ```

Why Composition Matters

Benefits: • Each function is small and testable • Pipelines are readable top-to-bottom • Reuse: `double` works alone or in any pipeline • Lazy: combine with `.lazy` to avoid intermediate allocations

Quick Check

Which Swift feature lets key paths be used directly as transformation functions in map?

Recap

Key takeaways: • Compose small functions into larger transformations • `>>>` or manual composition for reusable pipelines • Key paths as functions for clean property extraction • Partial application creates specialised functions • Combine with `.lazy` for zero-copy pipelines Next: lazy sequences.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Function Composition and Pipelines” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Function Composition and Pipelines”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Function Composition and Pipelines”?

Building data pipelines by composing small functions into larger transformations. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Function Composition and Pipelines” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Pure Functions and Side-Effect-Free Design
  2. Higher-Order Functions: map, flatMap, compactMap
  3. Function Composition and Pipelines
  4. Lazy Sequences for Efficiency
← Back to Swift Academy