Pure Functions Implementation in Swift
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Pure Functions runs in O(1) time and O(1) space, in Swift as in every other language.
- ✓struct plus let is the core: a struct is a value type, so assignment copies, and let forbids mutation of the binding.
- ✓Prefer struct over class until you actually need identity or inheritance, and prefer let over var everywhere, letting the compiler tell you where mutation is required.
- ✓The Swift trap: A struct containing a class reference is only shallowly immutable: let order = Order(...) forbids reassigning order.customer but not mutating the object it points at, so the value type gives no protection over the reference field.
- ✓Copy-on-write means passing an Array around is a retain, not an O(n) copy; the copy happens on first mutation when the buffer is not uniquely referenced, which isKnownUniquelyReferenced exposes.
What Pure Functions actually is
Pure Functions in Swift: what you actually reach for
Swift implementation
struct Line: Equatable { let sku: String; let qty: Int }
struct Order: Equatable {
let id: String
private(set) var lines: [Line]
// non-mutating: returns a new value, leaves the receiver alone
func adding(_ line: Line) -> Order {
Order(id: id, lines: lines + [line])
}
var total: Int { lines.reduce(0) { $0 + $1.qty } }
}
final class Customer { var name = "ada" }
struct Shallow { let customer: Customer } // let does NOT freeze customer.name
let s = Shallow(customer: Customer())
s.customer.name = "grace" // compiles and mutatesThink Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Swift-specific trap
Performance in Swift, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Swift type or library should I use for Pure Functions?
struct plus let is the core: a struct is a value type, so assignment copies, and let forbids mutation of the binding. Array, Dictionary, Set and String are all value types with copy-on-write. A method that mutates a struct must be marked mutating, which makes the write visible in the signature.
What is the most common Swift mistake when implementing Pure Functions?
A struct containing a class reference is only shallowly immutable: let order = Order(...) forbids reassigning order.customer but not mutating the object it points at, so the value type gives no protection over the reference field. The other surprise is that copy-on-write makes the copy invisible, an array assigned to a second variable shares storage until one of them writes, so a benchmark that only reads will not show the copy that a mutation later triggers.
How does Pure Functions actually perform in Swift?
Copy-on-write means passing an Array around is a retain, not an O(n) copy; the copy happens on first mutation when the buffer is not uniquely referenced, which isKnownUniquelyReferenced exposes. Small structs are passed in registers. The classic pathology is mutating a shared array inside a loop, where each iteration triggers a fresh full copy. Time complexity is O(1) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Student discounts on developer tools are collected on Noizz, tool by tool.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.