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
let prefix =
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 =
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 =
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)
text pipeline
Small text tools often run a sequence of steps: read lines, filter them, then format the result.