This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// Investigations on Protocols and function delegates | |
/* | |
The accepted way of doing things | |
*/ | |
protocol ProtoDelegate { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getOrElse<S>(f : @auto_closure () -> S)(a : S?) -> S { | |
if let r = a { | |
return r | |
} | |
else { | |
return f() | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import UIKit | |
import Foundation | |
import Swift | |
class MySingleton { | |
var amount : Float! | |
NewerOlder