Skip to content

Commit

Permalink
Remove in-place reduce(), see branch in-place-reduce-experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhunsley committed Jun 16, 2024
1 parent 352b2b5 commit 0c9f966
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 71 deletions.
64 changes: 1 addition & 63 deletions Sources/PatchedContent+Reduce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public extension PatchedContent {

/// Returns the content produced by applying the patches to the content
/// using the `patcher` protocol witness.
/// To use the default patcher for a PatchType, please instead use the convenience `reduce()`.
/// To use the default patcher for a PatchType, please instead use the convenience `reduced()`.
func reduced(_ patcher: Patchable<T>) throws -> T.ContentType {
var accumulatedReduceResult = content

Expand Down Expand Up @@ -67,66 +67,4 @@ public extension PatchedContent {
}
return accumulatedReduceResult
}

/// Convenience that calls reduce using the mutating patchable for the PatchType (i.e. T)
mutating func reduce() throws -> Void {
guard let mutatingPatcher = T.mutatingPatcher else {
throw PatchouliError<T>.mutatingReduceNotSupported
}
try reduce(mutatingPatcher)
}

/// Returns the content produced by applying the patches to the content
/// using the `patcher` protocol witness.
/// To use the default patcher for a PatchType, please instead use the convenience `reduce()`.
mutating func reduce(_ patcher: MutatingPatchable<T>) throws -> Void {
let originalContent = content

for item in contentPatches {
switch item.patchSpec {

case let .add(address):
guard var targetContent = item.contentPatch else { throw PatchouliError<T>.contentWasNil }

try targetContent.reduce(patcher)

guard let add = patcher.add else { throw PatchouliError<T>.mutatingAddNotSupported }
add(&content, targetContent.content, address)

case let .remove(address):
guard let remove = patcher.remove else { throw PatchouliError<T>.mutatingRemoveNotSupported }
remove(&content, address)

case let .replace(address):
guard var targetContent = item.contentPatch else { throw PatchouliError<T>.contentWasNil }

try targetContent.reduce(patcher)

guard let replace = patcher.replace else { throw PatchouliError<T>.mutatingReplaceNotSupported }
replace(&content, targetContent.content, address)

case let .move(fromAddress, toAddress):
guard let move = patcher.move else { throw PatchouliError<T>.mutatingMoveNotSupported }
move(&content, fromAddress, toAddress)

case let .test(expectedContent, address):
guard let test = patcher.test else { throw PatchouliError<T>.testNotSupported }

do {
try test(content, expectedContent, address)
}
catch let error as PatchouliError<T> {
switch error {
case .testFailed:
content = originalContent
default:
break
}
}

default:
break
}
}
}
}
8 changes: 0 additions & 8 deletions Tests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,11 @@ extension PatchedContent {

XCTAssertEqual(reduced, expectedContent,
"Didn't get expected content from reduced() (i.e. non-mutating reducer) in \(callingFunc)")

// we work on a copy so we don't have to mark this method as mutating
var mutableContentCopy = self
try mutableContentCopy.reduce()

XCTAssertEqual(mutableContentCopy.content, expectedContent,
"Didn't get expected content from reduce() (i.e. mutating reducer) in \(callingFunc)")
}

func assertReducersDoNotThrow() {
var mutableCopy = self
XCTAssertNoThrow(try mutableCopy.reduced())
XCTAssertNoThrow(try mutableCopy.reduce())
}
}

Expand Down

0 comments on commit 0c9f966

Please sign in to comment.