Skip to content

Instantly share code, notes, and snippets.

@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 {
@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 / 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 / Model.swift
Created July 19, 2016 03:52
Morse code data model
/// Represents an entire Morse encoded message.
struct EncodedMessage { let encodedTerms: [EncodedTerm] }
/// Represents a word or number consisting of Morse code symbols.
struct EncodedTerm { let symbols: [Symbol] }
/// Represents a character encoded with Morse code marks.
struct Symbol { let marks: [Mark] }
/// Represents an individual component of a Morse code symbol.
@ijoshsmith
ijoshsmith / anagrams.swift
Created June 19, 2016 22:09
Finding anagrams in Swift
func findAnagramsIn(words: [String]) -> [[String]] {
var signatureToAnagrams = [String: [String]]()
for word in words {
let signature = String(word.characters.sort())
if let anagrams = signatureToAnagrams[signature] {
signatureToAnagrams[signature] = anagrams + [word]
}
else {
signatureToAnagrams[signature] = [word]
}
@ijoshsmith
ijoshsmith / weather.dat
Created April 20, 2016 20:12
Weather Data kata in Elixir
Dy MxT MnT AvT HDDay AvDP 1HrP TPcpn WxType PDir AvSp Dir MxS SkyC MxR MnR AvSLP
1 88 59 74 53.8 0.00 F 280 9.6 270 17 1.6 93 23 1004.5
2 79 63 71 46.5 0.00 330 8.7 340 23 3.3 70 28 1004.5
3 77 55 66 39.6 0.00 350 5.0 350 9 2.8 59 24 1016.8
4 77 59 68 51.1 0.00 110 9.1 130 12 8.6 62 40 1021.1
5 90 66 78 68.3 0.00 TFH 220 8.3 260 12 6.9 84 55 1014.4
6 81 61 71 63.7 0.00 RFH 030 6.2 030 13 9.7 93 60 1012.7
7 73 57 65 53.0 0.00 RF 050 9.5 050 17 5.3 90 48 1021.8
8 75 54 65 50.0 0.00 FH 160 4.2 150 10 2.6 93 41 1026.3
@ijoshsmith
ijoshsmith / Dictionary+KeysForValue.swift
Created April 14, 2016 15:57
Find keys mapped to a value in Swift dictionary
extension Dictionary where Value: Equatable {
/// Returns all keys mapped to the specified value.
/// ```
/// let dict = ["A": 1, "B": 2, "C": 3]
/// let keys = dict.keysForValue(2)
/// assert(keys == ["B"])
/// assert(dict["B"] == 2)
/// ```
func keysForValue(value: Value) -> [Key] {
return flatMap { (key: Key, val: Value) -> Key? in
@ijoshsmith
ijoshsmith / higher-order-funcs.swift
Last active April 8, 2017 12:51
Higher-order functions in Swift
//
// main.swift
// HigherOrderFunctions
//
// Created by Joshua Smith on 12/6/15.
// Copyright © 2015 iJoshSmith. All rights reserved.
//
/*
This file contains simple implementations of several
@ijoshsmith
ijoshsmith / compress.swift
Created November 26, 2015 05:59
Swift array compression programming puzzle
import Foundation
/**
Returns a compacted array, with repeated values coalesced.
Example: [a, a, b, c, c, c] yields [(a, 2), (b, 1), (c, 3)]
*/
func compressArray<T: Comparable>(input: [T]) -> [(T, Int)] {
// TODO - implement this function
return []
}
@ijoshsmith
ijoshsmith / fold.hs
Created June 3, 2015 15:51
foldr vs. foldl
> foldr (-) 0 [3, 2, 1]
2
> (3 - (2 - (1 - 0)))
2
> foldl (-) 0 [3, 2, 1]
-6
> (((0 - 3) - 2) - 1)