Skip to content

Instantly share code, notes, and snippets.

@ijoshsmith
ijoshsmith / TextTransformation.swift
Last active July 19, 2016 04:23
Transform Morse code data model to text
func createMorseCodeText(from encodedMessage: EncodedMessage) -> String {
let transformation = MorseTransformation(
dot: ".",
dash: "-",
markSeparator: "",
symbolSeparator: " ",
termSeparator: "\n")
let characters = transformation.apply(to: encodedMessage)
return characters.joinWithSeparator("")
}
@ijoshsmith
ijoshsmith / TransmissionState.swift
Last active July 19, 2016 04:23
Transform Morse code data model to on/off states with relative durations
enum TransmissionState {
typealias RelativeDuration = Int
case On(RelativeDuration)
case Off(RelativeDuration)
static func createStates(from encodedMessage: EncodedMessage)
-> [TransmissionState] {
let transformation = MorseTransformation(
dot: TransmissionState.On(1),
dash: TransmissionState.On(3),
@ijoshsmith
ijoshsmith / MorseTransformation.swift
Created July 19, 2016 03:59
Uses the Concealment pattern to enable transforming the Morse code data model
/// Converts an `EncodedMessage` to an alternate representation.
struct MorseTransformation<T> {
let dot, dash, markSeparator, symbolSeparator, termSeparator: T
func apply(to encodedMessage: EncodedMessage) -> [T] {
return encodedMessage.apply(self)
}
}
private extension EncodedMessage {