Skip to content

Instantly share code, notes, and snippets.

@Sajjon
Created August 17, 2020 14:10
Show Gist options
  • Save Sajjon/1962c9d104d2b1faf85476fbad8cf32e to your computer and use it in GitHub Desktop.
Save Sajjon/1962c9d104d2b1faf85476fbad8cf32e to your computer and use it in GitHub Desktop.
Cachable ScanJob
struct ScanJob {
let runContext: RunContext
}
// MARK: Job
extension ScanJob: CacheableJob {
typealias Input = RunContext
typealias Output = ScannedLines
func validateCached(_ cached: Output) throws {
guard cached.numberOfLines >= runContext.numberOfLinesToScan else {
throw Error.notEnoughLinesScanned
}
}
func newWork(input _: Input) throws -> Output {
let inputFileName = runContext.fileNameOfInputCorpus
let file = try Cacher.currentDirectoryPath.openFile(named: inputFileName)
let lineReader = try LineScanner(file: file)
print("✅ Starting to scan lines")
let numberOfLinesToScan = runContext.numberOfLinesToScan
var scannedLines = [ScannedLine]()
readlines: for lineIndex in 0...numberOfLinesToScan {
guard let rawLine: String = lineReader.nextLine() else {
if lineIndex < numberOfLinesToScan {
throw Error.notEnoughLinesScanned
}
break readlines
}
let scannedLine = ScannedLine(lineFromCorpus: rawLine, positionInCorpus: lineIndex)
scannedLines.append(scannedLine)
}
return ScannedLines(scannedLines)
}
}
extension ScanJob {
enum Error: Swift.Error, Equatable {
case notEnoughLinesScanned
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment