Skip to content

Instantly share code, notes, and snippets.

View Sajjon's full-sized avatar
💭
FOSS|Rust&Swift|Principal Cryptography & Middleware Engineer@RDX Works/Radix DLT

Alexander Cyon Sajjon

💭
FOSS|Rust&Swift|Principal Cryptography & Middleware Engineer@RDX Works/Radix DLT
View GitHub Profile
@Sajjon
Sajjon / ScanJob.swift
Created August 17, 2020 14:10
Cachable ScanJob
struct ScanJob {
let runContext: RunContext
}
// MARK: Job
extension ScanJob: CacheableJob {
typealias Input = RunContext
typealias Output = ScannedLines
@Sajjon
Sajjon / CacheableJob.swift
Created August 17, 2020 14:06
Caching results from jobs for faster run time.
protocol CacheableJob: Job where Output: Codable {
var runContext: RunContext { get }
var fileName: String { get }
func newWork(input: Input) throws -> Output
func validateCached(_ cached: Output) throws
}
struct CachedJob<CachedData>: Codable where CachedData: Codable {
let fileNameOfInputCorpus: String
@Sajjon
Sajjon / BIP39.swift
Created August 17, 2020 13:58
createWordList
func createWordList() throws {
let pipeline = Pipeline {
ScanJob()
ParseJob()
WordLengthJob()
WhitelistedPOSTagsJob()
HomonymJob()
}
@Sajjon
Sajjon / BuildPipeline.swift
Created August 17, 2020 13:47
@_functionBuilder support for Pipeline.
// MARK: FunctionBuilder
@_functionBuilder
struct BuildPipeline {
static func buildBlock<A>(_ a: A) -> Pipeline<A.Input, A.Output>
where
A: Job
{
Pipeline(description: descriptionOf(jobs: [a])) { input in
@Sajjon
Sajjon / Pipeline.swift
Created August 17, 2020 12:58
A suite of jobs piped together.
struct Pipeline<Input, Output>: Job, CustomStringConvertible {
let description: String
private let _getOutput: (Input) throws -> Output
init(description: String, _ getOutput: @escaping (Input) throws -> Output) {
self.description = description
self._getOutput = getOutput
}
}
@Sajjon
Sajjon / PipeOperator.swift
Last active August 17, 2020 12:54
Operator allowing for chaining - piping - "jobs" together.
precedencegroup Pipe {
higherThan: DefaultPrecedence
associativity: left
assignment: true
}
infix operator |>: Pipe
func |> <J>(input: J.Input, job: J) throws -> J.Output where J: Job {
try job.work(input: input)
}
@Sajjon
Sajjon / Job.swift
Created August 17, 2020 12:51
A runnable task performing some work.
protocol Job {
associatedtype Input
associatedtype Output
func work(input: Input) throws -> Output
}
@Sajjon
Sajjon / LineFromCorpusFromLine.swift
Created August 15, 2020 14:39
LineFromCorpusFromLine
protocol LineFromCorpusConvertible: CustomStringConvertible, Codable {
/// The word, on lowercased form
var wordForm: WordForm { get }
/// Part of speech tag, only first major tag, .e.g from line `han PN.UTR.SIN.DEF.SUB`, we only save `PN`
var partOfSpeechTag: PartOfSpeech { get }
/// Base form(s) of word
var lemgrams: Lemgrams { get }
@Sajjon
Sajjon / WordLengthLine.swift
Created August 15, 2020 14:37
A line which word has suitable length
/// A line which word has suitable length
struct WordLengthLine: LineFromCorpusFromLine, Hashable {
typealias FromLine = ParsedLine
let line: FromLine
}
@Sajjon
Sajjon / ParsedLine.swift
Created August 15, 2020 13:47
A parsed line from the Swedish corpus at Språkbanken.
/// A parsed line from some scanned line in the corpus.
struct ParsedLine: Codable, Hashable {
let wordForm: WordForm
let partOfSpeechTag: PartOfSpeech
let lemgrams: Lemgrams
let isCompoundWord: Bool
let numberOfOccurencesInCorpus: Int
let relativeFrequencyPerOneMillion: Int