A text pipeline keeps lines with a chosen prefix and joins the result.

Filter lines by prefix

prefix
text_pipeline.swift
Replay: real traced execution (multi-file project)
let prefix = "TODO"
let lines = [
    "TODO write outline",
    "DONE review draft",
    "TODO publish page"
]
var matches: [String] = []

for line in lines {
    if line.hasPrefix(prefix) {
        matches.append(line)
    }
}

let output = matches.joined(separator: " | ")

print(output)
let prefix = "DONE"
let lines = [
    "TODO write outline",
    "DONE review draft",
    "TODO publish page"
]
var matches: [String] = []

for line in lines {
    if line.hasPrefix(prefix) {
        matches.append(line)
    }
}

let output = matches.joined(separator: " | ")

print(output)
let prefix = "NOTE"
let lines = [
    "TODO write outline",
    "DONE review draft",
    "TODO publish page"
]
var matches: [String] = []

for line in lines {
    if line.hasPrefix(prefix) {
        matches.append(line)
    }
}

let output = matches.joined(separator: " | ")

print(output)
  1. prefix ← TODO, lines ← ["TODO write outline", "DONE review draft", "TODO publish page"]

    1let prefix→ TODO = "TODO"  //@prefix="DONE", "NOTE"2let lines→ ["TODO write outline", "DONE review draft", "TODO publish page"] = [3    "TODO write outline",4    "DONE review draft",5    "TODO publish page"6]7var matches→ []: [String] = []
  2. for line in lines

    pass 1 of 3
    9for lineTODO write outline in lines["TODO write outline", "DONE review draft", "TODO publish page"] {10    if line.hasPrefix(prefix) {
    All 3 passes — pass 1 is the card above
    passlineprefixmatches
    1TODO write outlineTODO[]
    2DONE review draft
    3TODO publish pageTODO["TODO write outline"]
  3. if line.hasPrefix(prefix)

    pass 1 of 2
    9for line in lines {10    if lineTODO write outline.hasPrefix(prefixTODO) {11        matches[].append(lineTODO write outline)12    }
  4. if line.hasPrefix(prefix)

    pass 2 of 2
    9for line in lines {10    if lineTODO publish page.hasPrefix(prefixTODO) {11        matches["TODO write outline"].append(lineTODO publish page)12    }
  5. output ← TODO write outline | TODO publish page

    15let output→ TODO write outline | TODO publish page = matches["TODO write outline", "TODO publish page"].joined(separator: " | ")1617print(outputTODO write outline | TODO publish page)
    outputTODO write outline | TODO publish page
  1. prefix ← DONE, lines ← ["TODO write outline", "DONE review draft", "TODO publish page"]

    1let prefix→ DONE = "DONE"2let lines→ ["TODO write outline", "DONE review draft", "TODO publish page"] = [3    "TODO write outline",4    "DONE review draft",5    "TODO publish page"6]7var matches→ []: [String] = []
  2. for line in lines

    pass 1 of 3
    9for lineTODO write outline in lines["TODO write outline", "DONE review draft", "TODO publish page"] {10    if line.hasPrefix(prefix) {
    All 3 passes — pass 1 is the card above
    passlineprefixmatches
    1TODO write outline
    2DONE review draftDONE[]
    3TODO publish page
  3. if line.hasPrefix(prefix)

    9for line in lines {10    if lineDONE review draft.hasPrefix(prefixDONE) {11        matches[].append(lineDONE review draft)12    }
  4. output ← DONE review draft

    15let output→ DONE review draft = matches["DONE review draft"].joined(separator: " | ")1617print(outputDONE review draft)
    outputDONE review draft
  1. prefix ← NOTE, lines ← ["TODO write outline", "DONE review draft", "TODO publish page"]

    1let prefix→ NOTE = "NOTE"2let lines→ ["TODO write outline", "DONE review draft", "TODO publish page"] = [3    "TODO write outline",4    "DONE review draft",5    "TODO publish page"6]7var matches→ []: [String] = []
  2. for line in lines

    pass 1 of 3
    9for lineTODO write outline in lines["TODO write outline", "DONE review draft", "TODO publish page"] {10    if line.hasPrefix(prefix) {
    All 3 passes — pass 1 is the card above
    passline
    1TODO write outline
    2DONE review draft
    3TODO publish page
  3. output ← (empty)

    15let output→ (empty) = matches[].joined(separator: " | ")1617print(output(empty))
text pipeline Small text tools often run a sequence of steps: read lines, filter them, then format the result.