Skip to content

Instantly share code, notes, and snippets.

Andy Calderbank plumhead

Block or report user

Report or block plumhead

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@plumhead
plumhead / NSTextViewExtensions.swift
Created Nov 13, 2015
Useful NSTextView extensions
View NSTextViewExtensions.swift
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 Sep 24, 2015
Playing with Swift Pattern Matching for Mathematical Expressions (run in playground)
View SwiftExpr.swift
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 Sep 15, 2015
String extension to find the layout size of a String with specified attributes.
View StringSize.swift
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)
View gist:832ee5226f26f76e8d57
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 Apr 29, 2015
MKCoordinateRegion extension to determine region needed to fit a set of locations with zoom factor
View gist:c93bd759f986752228df
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))
View gist:f8452beed6be9c224771
import UIKit
// Investigations on Protocols and function delegates
/*
The accepted way of doing things
*/
protocol ProtoDelegate {
View gist:5b02c6eb08c03a266d44
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 {
View gist:07ec4be10c5c968304c4
func getOrElse<S>(f : @auto_closure () -> S)(a : S?) -> S {
if let r = a {
return r
}
else {
return f()
}
}
View gist:b5ef3065aabcb8a6e15a
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
View gist:f5947ab7cf42107bb87e
// Playground - noun: a place where people can play
import UIKit
import Foundation
import Swift
class MySingleton {
var amount : Float!
You can’t perform that action at this time.