Practical Swift Programs
Text Pipeline
A text pipeline keeps lines with a chosen prefix and joins the result.
Filter lines by 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)
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] = []for line in lines
pass 1 of 39for 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 pass lineprefixmatches1 TODO write outline TODO [] 2 DONE review draft — — 3 TODO publish page TODO ["TODO write outline"] if line.hasPrefix(prefix)
pass 1 of 29for line in lines {10 if lineTODO write outline.hasPrefix(prefixTODO) {11 matches[].append(lineTODO write outline)12 }if line.hasPrefix(prefix)
pass 2 of 29for line in lines {10 if lineTODO publish page.hasPrefix(prefixTODO) {11 matches["TODO write outline"].append(lineTODO publish page)12 }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
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] = []for line in lines
pass 1 of 39for 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 pass lineprefixmatches1 TODO write outline — — 2 DONE review draft DONE [] 3 TODO publish page — — if line.hasPrefix(prefix)
9for line in lines {10 if lineDONE review draft.hasPrefix(prefixDONE) {11 matches[].append(lineDONE review draft)12 }output ← DONE review draft
15let output→ DONE review draft = matches["DONE review draft"].joined(separator: " | ")1617print(outputDONE review draft)outputDONE review draft
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] = []for line in lines
pass 1 of 39for 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 pass line1 TODO write outline 2 DONE review draft 3 TODO publish page 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.