Hi, I’m Sam

This is my blog. I also have a website thing.

Git + Redis Backed Blog

Posted on

I switched back to the old blog I was using before Roon (this blogging platform I used to run). Right before Roon, I had all of these fun ideas for the nerdy blogging platform that I wanted.

The main idea was all of my posts lived in their own repo. This is great for a bunch of reason. Being separate from my blog's source code is nice since it changes every few years. Even when I was blogging on Roon and Ghost, I kept this up to date (with some scripts). I saw a talk from one of the guys that works on Archive.org and was really inspired to start saving as much as I can.

Since my posts are in their own repo, a simple post-commit hook can update my blog. GitHub simply posts to an enpoint on my blog that causes it to reimport my posts into Redis. Another added benefit is people can PR typo fixes. When I click the merge button on GitHub, the webhook fires and automatically updates the post on my site. Neat!

Continue reading →

String Homogeneousness Algorithms

Posted on

Today, I tweeted the following code while I was working on Whiskey:

import Foundation

extension String {
  var isHomogeneous: Bool {
    var homogeneous = true
    var character: NSString?
    enumerateSubstringsInRange(Range(start: startIndex, end: endIndex), options: [.ByComposedCharacterSequences]) { substring, _, _, stop in
      if let character = character {
        if character != substring {
          homogeneous = false
          stop = true
        }
      } else {
        character = substring
      }
    }

    return homogeneous
  }
}

It just checks to see if all of the characters in a string are all the same. Here's the test:

Continue reading →

Network Testing in Swift with DVR

Posted on

Networking testing is hard. There’s a few approaches. The most common I’ve seen is stubbing requests. OCMock is a common approach.

Personally, I’ve never been a fan of mocks and stubs. At some point you just end up testing your mocks and stubs instead of your real code. When it comes to testing, I want unit tests to test logic and integration tests to test compositions.

I think focusing on testing everything in isolation isn’t great. If you have stuff that is hard to test in isolation, either it should be redesigned to more encapsulated or due to the nature of it, you need to test it at a higher level.

Continue reading →

SyntaxKit

Posted on

I sat down to work on Whiskey the other day and go super side tracked. I have this new master plan to greatly improve Whiskey's markdown parsing, but it's a lot of work so I started procrastiworking. I noticed some bugs in SyntaxKit, what I wrote so Whiskey can do code coloring for things other than Markdown, and decided to start fixing them. Because I was procrastiworking, the logical first step was to rewrite it in Swift 2 :)

Since this isn't terribly specific to Whiskey, I decided to open source SyntaxKit. It makes TextMate-style syntax highlighting easy. It works on iOS & OS X. There aren't any dependencies besides system frameworks. Boom.

Carthage is the recommended way to install SyntaxKit. Add the following to your Cartfile:

Continue reading →

Automatic UI Updates with Value Types in Swift

Posted on

Value types are one of my favorite things in Swift. At first, I was resistant. It’s a much different way of thinking. Let’s look at a simple example that really shows the power.

I was recently working on a little control for entering in numbers on Apple Watch. Here's the code:

public struct NumberPad: Printable {

    // MARK: - Properties

    public var value: Double

    public let decimalPlaces: UInt

    public var description: String {
        let formatter = NSNumberFormatter()
        formatter.numberStyle = .CurrencyStyle
        formatter.maximumFractionDigits = Int(decimalPlaces)
        formatter.minimumFractionDigits = currencyFormatter.maximumFractionDigits
        return formatter.stringFromNumber(value)!
    }

    public var isEmpty: Bool {
        return value == 0.0
    }


    // MARK: - Initializers

    public init(value: Double = 0.0, decimalPlaces: UInt = 2) {
        self.value = value
        self.decimalPlaces = decimalPlaces
    }


    // MARK: - Manipulation

    public mutating func insert(place: Int) {
        let offset = pow(10.0, Double(decimalPlaces))
        var int = Int(value * offset)
        int *= 10
        int += place
        value = Double(int) / offset
    }

    public mutating func deleteBackwards() {
        let offset = pow(10.0, Double(decimalPlaces))
        var int = Int(value * offset)
        int /= 10
        value = Double(int) / offset
    }
}

Continue reading →