Skip to content

Instantly share code, notes, and snippets.

@Nadohs
Nadohs / gist:30f4ec3cb9806223b6f4
Created August 25, 2015 19:12
range comparing...
<>, both not included in range,
<=>, both included
<==, left not included,
==> right not included
infix operator <> {}
infix operator <=> {}
infix operator ==> {}
infix operator <== {}
@Nadohs
Nadohs / gist:98c5db1f4125ce27309d
Last active August 29, 2015 14:28
range compare operator
func ~= <T:Comparable>(lhs:T,rhs:(T,T)) -> Bool{
if lhs < rhs.0{
return false
}
if lhs > rhs.1{
return false
@Nadohs
Nadohs / gist:d2bbd7a070f0b98fb68f
Created August 17, 2015 00:09
Int Casting-Initializers
extension Int {
/// Construct an instance that approximates `other`.
public init(_ other: Float)
/// Construct an instance that approximates `other`.
public init(_ other: Double)
/// Construct an instance that approximates `other`.
public init(_ other: Float80)
}
@Nadohs
Nadohs / gist:c86990ccbe9ddeea12a6
Created August 16, 2015 23:48
Dot Property Number Casting
//Number classes use initializers, that take in other number types to convert between types
//Essentially this is what is going on:
//`Int(4.5)` ~is just this~ `Int.init(4.5)`
//Which is using `init(_ value:Double)` definid with in the `Int` class
//Using this knoledge we can optimize our conersions with pattern matching
import CoreGraphics //For CGFloat
@Nadohs
Nadohs / gist:77dbb03707fdaaf036e3
Last active August 29, 2015 14:26
checks if number overflows using ?=
func ?= <T:IntNumberConvertible, U:NumberConvertible>(var lhs: T, rhs: U) -> T{
print("INTNUMBER")
let max :Double = T.max.convert()
let test :Double = rhs.convert()
if test > max{
assert(false, "Conversion Loss")
}
@Nadohs
Nadohs / gist:ea7ea393ba4fb8b35fa2
Created July 7, 2015 02:03
defer syntax in Swift 1.2
class defering{
// func g <T>(T? -> T?){
//
// }
var defers:[(Void -> Void)] = []
func fer( df:Void -> Void){
defers.append((df));
}
@Nadohs
Nadohs / gist:9149c405a1f53a6dee23
Created June 17, 2015 06:56
where clause chaining 0_0
extension DictionaryLiteralConvertible where Key == String, Value == AnyObject {
func magicFilter(){
print("blah")
}
}
//WORKS
let test1 = [String : AnyObject]()
test1.magicFilter()
@Nadohs
Nadohs / gist:538579fa7117157a78b8
Created May 13, 2015 20:14
get list of available fonts in swift
class func findCustomFonts(){
for family in UIFont.familyNames()
{
NSLog("%@", family as! String);
let fontNames = UIFont.fontNamesForFamilyName(family as! String);
fontNames.map{
println("\($0)")
}
@Nadohs
Nadohs / numExtension.swift
Last active August 29, 2015 14:20
Solution for working with number types in swift
/// var num:Double = 12;
/// var num2:Float = num.f;
/// var num3:CGFloat = num.c;
/// var num4:Int = num.i;
/// var num5:Double = num2.d;
import UIKit;
extension Double{
var c:CGFloat{