Skip to content

Instantly share code, notes, and snippets.

@airspeedswift
airspeedswift / OneSidedRanges.swift
Last active April 21, 2023 17:14
One-sided Range operators
postfix operator ..< { }
prefix operator ..< { }
struct RangeStart<I: ForwardIndexType> { let start: I }
struct RangeEnd<I: ForwardIndexType> { let end: I }
postfix func ..<<I: ForwardIndexType>(lhs: I) -> RangeStart<I>
{ return RangeStart(start: lhs) }
prefix func ..<<I: ForwardIndexType>(rhs: I) -> RangeEnd<I>
@twostraws
twostraws / gist:3d673d4eba36de173f6f
Last active August 29, 2015 14:23
Love Wins in Swift
//
// loveWins(): a simple function that accepts a UIImage and
// returns the same image blended with the rainbow flag
// of the LGBT pride movement.
//
// This is released for pedagogical reasons (I've tried to make
// the code as easy to follow as possible!) but you're welcome
// to use it for any purpose – consider the code yours.
//
// If you're using Xcode 7 / Swift 2, you need to make a tiny
private extension GeneratorType {
mutating func any(@noescape pred: Element -> Bool) -> Bool {
return next().map { el in pred(el) || any(pred) } ?? false
}
mutating func all(@noescape pred: Element -> Bool) -> Bool {
return next().map { el in pred(el) && all(pred) } ?? true
}
}
public extension SequenceType {
@capttaco
capttaco / Fetchable.swift
Last active May 3, 2019 17:28
A utility protocol for custom NSManagedObjects that makes querying contexts simpler and more convenient. Requires Swift 2.
import CoreData
protocol Fetchable
{
typealias FetchableType: NSManagedObject
static func entityName() -> String
static func objectsInContext(context: NSManagedObjectContext, predicate: NSPredicate?, sortedBy: String?, ascending: Bool) throws -> [FetchableType]
static func singleObjectInContext(context: NSManagedObjectContext, predicate: NSPredicate?, sortedBy: String?, ascending: Bool) throws -> FetchableType?
static func objectCountInContext(context: NSManagedObjectContext, predicate: NSPredicate?) -> Int
@emreberge
emreberge / KeyPath.h
Last active September 3, 2015 12:15
Compiler help for keyPaths!
#define KeyPath(keyPath)\
^NSString *(void) {\
__unused __typeof__(keyPath) x;\
return [@#keyPath substringFromIndex:([@#keyPath rangeOfString:@"."].location + 1)];\
}()
@zats
zats / script.swift
Last active March 5, 2021 01:32
Update all your plugins for the latest Xcode beta with a single
#!/usr/bin/env xcrun swift
// $ chmod +x script.swift
// $ ./script.swift
// or $ ./script.swift -xcode=/Applications/Xcode-beta.app
import Foundation
@noreturn private func failWithError(message: String) {
print("🚫 \(message)")
// http://blog.krzyzanowskim.com
import Cocoa
struct ChunkSequence<Element>: SequenceType {
let chunkSize: Array<Element>.Index
let collection: Array<Element>
func generate() -> AnyGenerator<ArraySlice<Element>> {
var offset:Array<Element>.Index = collection.startIndex
@andymatuschak
andymatuschak / States-v3.md
Last active May 1, 2024 12:32
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

@irace
irace / TabComponent.swift
Last active December 22, 2020 15:38
Easily roll your own `UITabBarController` alternatives. Here’s all the logic you need without assuming anything about your UI.
/**
* A class that can be part of a tabbed navigational interface (expected to be a `UIViewController` but can also be a
* coordinator that proxies through to an underlying controller).
*/
public protocol TabComponent {
/// The tab metadata
var tabItem: TabItem { get }
var viewController: UIViewController { get }
}
@alessaba
alessaba / PlaygroundsFrameworks.swift
Last active July 29, 2023 21:47
List of available frameworks in Swift Playgrounds over the years
// Swift Playgrounds Beta 1.0
import AVFoundation
import AVKit
import Accelerate
import Accounts
import AudioToolbox
import AudioUnit
import CFNetwork
import CoreAudio
import CoreAudioKit