Skip to content

Instantly share code, notes, and snippets.

@sharplet
sharplet / NSArray+indexPathKeyedSubscript.m
Last active December 17, 2015 18:39
Category on NSArray to allow subscripting via an NSIndexPath.
//
// Category on NSArray to allow subscripting via an NSIndexPath.
//
// Adam Sharp
// May 27, 2013
//
// Given this array:
//
// NSArray *matrix = @[
// @[ @1, @2 ],
@sharplet
sharplet / Actor.swift
Last active April 5, 2017 07:54
Simple GCD-based actors in Swift
import Dispatch
/// Wraps some `Base` type so that all method calls become
/// "message sends", e.g., `async { $0.foo() }` or `sync { $0.bar() }`.
public final class Actor<Base> {
private var instance: Base
private let queue: DispatchQueue
public init(_ instance: Base, target: DispatchQueue? = nil) {
self.instance = instance
@sharplet
sharplet / ConcatenateSequence.swift
Created February 9, 2015 10:25
Generic concatenation of sequences in Swift
func + <S: SequenceType, E where S.Generator.Element == E> (lhs: S, rhs: S) -> GeneratorOf<E> {
var (g, h) = (lhs.generate(), rhs.generate())
return GeneratorOf {
g.next() ?? h.next()
}
}
@sharplet
sharplet / BitMask.swift
Created July 11, 2017 15:50
Playing around with a high-level API for working with bit masks
struct BitMask<T: FixedWidthInteger>: RandomAccessCollection {
typealias SubSequence = BitMask<T>
var rawValue: T {
var rawValue: T = 0
for bit in self {
rawValue |= bit
}
return rawValue
}
@sharplet
sharplet / LineSequence.swift
Created November 8, 2017 20:03
Getting a sequence of lines from a Swift string
import Foundation
let text = """
Hello
World
"""
extension String {
var lines: AnySequence<Substring> {
let string = self
@sharplet
sharplet / README.md
Last active June 8, 2018 22:10
Unearned Stack Overflow badges Dashing widget

Badge Overflow

An exceptionally handsome way to track your Stack Overflow badges.

Created by Adam & Stephanie Sharp.

Unearned Badges widget

A Dashing widget that tracks your progress toward unearned badges on Stack Overflow.

@sharplet
sharplet / ContentViewEmbedding.swift
Created February 6, 2019 16:32
A protocol for view controllers with a replaceable content view controller
import UIKit
/// For a view controller that has no content of its own, implement like so:
///
/// extension RootViewController: ContentViewEmbedding {
/// var contentView: UIView! {
/// return view
/// }
/// }
protocol ContentViewEmbedding {
@sharplet
sharplet / Example.swift
Last active May 11, 2019 13:00
Mutable property lenses for ReactiveSwift with key paths
struct User {
var name: String
}
let currentUser = MutableProperty(User(name: "Foo"))
currentUser[\.name] <~ nameTextField.reactive.textValues
@sharplet
sharplet / KeychainItem.swift
Created April 25, 2020 23:57
A lightweight keychain wrapper for querying the keychain databse
// Copyright (c) 2019–20 Adam Sharp and thoughtbot, inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
@sharplet
sharplet / Channel.swift
Created August 13, 2020 13:16
A simple generic unbuffered channel using NSCondition
import Foundation
class Channel<Message> {
private enum State {
case empty
case readyToReceive
case full(Message)
}
private let condition: NSCondition