Skip to content

Instantly share code, notes, and snippets.

View plumhead's full-sized avatar

Andy Calderbank plumhead

View GitHub Profile
@plumhead
plumhead / NSTextViewExtensions.swift
Created November 13, 2015 16:38
Useful NSTextView extensions
extension NSTextView {
func rectForRange(range: NSRange) -> NSRect? {
guard let layout = self.layoutManager, container = self.textContainer else {return nil}
let rng = layout.glyphRangeForCharacterRange(range, actualCharacterRange: nil)
let rct = layout.boundingRectForGlyphRange(rng, inTextContainer: container)
return NSOffsetRect(rct, self.textContainerOrigin.x, self.textContainerOrigin.y)
}
func rectForSelectedRange() -> NSRect? {
return rectForRange(self.selectedRange())
@plumhead
plumhead / SwiftExpr.swift
Created September 24, 2015 15:22
Playing with Swift Pattern Matching for Mathematical Expressions (run in playground)
import Cocoa
//: # An exercise in specifying and simplifying Maths functions using Swift
/*: This is cut-down version of some code I've been working on to parse, simplify and display mathematical expressions. The source below shows some of the power of pattern matching and (recursive) enumerated types within Swift in defining and evaluating the expression domain. Note, it doesn't cover all cases but is intended to give a flavour of what can be achieved.
No optimisations applied
*/
//: # Supporting functions
//: ### Function Pipeline
@plumhead
plumhead / StringSize.swift
Created September 15, 2015 13:34
String extension to find the layout size of a String with specified attributes.
extension String {
func size(withAttributes attrs: [String:AnyObject], constrainedTo box: NSSize) -> NSRect {
let storage = NSTextStorage(string: self)
let container = NSTextContainer(containerSize: NSSize(width: box.width, height: box.height))
let layout = NSLayoutManager()
layout.addTextContainer(container)
storage.addLayoutManager(layout)
storage.addAttributes(attrs, range: NSMakeRange(0, storage.length))
container.lineFragmentPadding = 0.0
let _ = layout.glyphRangeForTextContainer(container)
@plumhead
plumhead / gist:832ee5226f26f76e8d57
Created April 29, 2015 15:01
Pairwise in Swift
func pairwise<T>(d: [T]) -> [(T,T)] {
switch d.count {
case 0: return []
case 1: return []
case let n:
let r = [(d[0],d[1])]
return r + pairwise(Array(d[1..<n]))
}
}
@plumhead
plumhead / gist:c93bd759f986752228df
Created April 29, 2015 08:17
MKCoordinateRegion extension to determine region needed to fit a set of locations with zoom factor
extension MKCoordinateRegion {
static func region(forLocations waypoints: [CLLocation], zoomPercent: Double = 0.2) -> MKCoordinateRegion {
var r = MKMapRectNull
for wp in waypoints {
var point = MKMapPointForCoordinate(wp.coordinate)
r = MKMapRectUnion(r, MKMapRectMake(point.x, point.y, 0, 0))
}
let zoomed = MKMapRectMake(r.origin.x-r.size.width*zoomPercent, r.origin.y-r.size.height*zoomPercent, r.size.width*(1+zoomPercent*2), r.size.height*(1+zoomPercent*2))
@plumhead
plumhead / gist:f8452beed6be9c224771
Created September 7, 2014 12:00
Swift Function Delegates
import UIKit
// Investigations on Protocols and function delegates
/*
The accepted way of doing things
*/
protocol ProtoDelegate {
class Times2Generator : Generator {
typealias Element = (original: Int,timestwo: Int)?
func next() -> Element? {
if source.count > 0 {
let result = self.source[0]
source = Array(source[1..source.count])
return (result,result * 2)
}
else {
@plumhead
plumhead / gist:07ec4be10c5c968304c4
Last active August 29, 2015 14:02
Pipe and Chains!
func getOrElse<S>(f : @auto_closure () -> S)(a : S?) -> S {
if let r = a {
return r
}
else {
return f()
}
}
operator infix |> {associativity left precedence 140}
func |> <T,U> (left: @auto_closure () -> T,right: T -> U) -> U {
return right(left())
}
func fold<S,T>(f : (S,T) -> S,acc: S)(s : Array<T>) -> S {
if s.isEmpty {
return acc
@plumhead
plumhead / gist:f5947ab7cf42107bb87e
Last active August 29, 2015 14:02
Swift Singletons
// Playground - noun: a place where people can play
import UIKit
import Foundation
import Swift
class MySingleton {
var amount : Float!