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 { | |
func apply<T>(transformation: MorseTransformation<T>) -> [T] { | |
return encodedTerms | |
.map { $0.apply(transformation) } | |
.joinWithSeparator([transformation.termSeparator]) | |
.toArray() | |
} | |
} | |
private extension EncodedTerm { | |
func apply<T>(transformation: MorseTransformation<T>) -> [T] { | |
return symbols | |
.map { $0.apply(transformation) } | |
.joinWithSeparator([transformation.symbolSeparator]) | |
.toArray() | |
} | |
} | |
private extension Symbol { | |
func apply<T>(transformation: MorseTransformation<T>) -> [T] { | |
return marks | |
.map { $0.apply(transformation) } | |
.joinWithSeparator([transformation.markSeparator]) | |
.toArray() | |
} | |
} | |
private extension Mark { | |
func apply<T>(transformation: MorseTransformation<T>) -> [T] { | |
return [self == .Dot ? transformation.dot : transformation.dash] | |
} | |
} | |
private extension JoinSequence { | |
func toArray() -> [Base.Generator.Element.Generator.Element] { | |
return Array(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment