Skip to content

Instantly share code, notes, and snippets.

extension SignedNumberType {
/// The absolute value of `self`.
var abs: Self {
return self < 0 ? -self : self
}
}
extension AbsoluteValuable {
/// The absolute value of `self`.
var abs: Self {
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi enim lacus, ullamcorper in gravida a, semper id dolor. Mauris quis metus id"
extension String {
func split(separator: Character, maxSplit: Int = .max, allowEmptySlices: Bool = false) -> [String] {
return characters.split(separator, maxSplit: maxSplit, allowEmptySlices: allowEmptySlices).map(String.init)
}
}
let words = try string.split(" ")
let counts = words.map { $0.characters.count }
@natecook1000
natecook1000 / lazyPrimes.swift
Created August 28, 2015 06:34
Lazily-generated infinite sequence of primes
/// An infinite sequence of positive integers.
struct PostiveIntegers: SequenceType {
typealias Generator = AnyGenerator<Int>
func generate() -> Generator {
var n = 0
return anyGenerator {
return ++n
}
}
import Foundation
/*
Toying with tools to help read binary formats.
I've seen lots of approaches in swift that create
an intermediate object per-read (usually another NSData)
but even if these are lightweight under the hood,
it seems like overkill. Plus this taught me about <()>
*/
for (key, value) in components {
if (value === f) {
let component = components.removeValueForKey(key)
println(component)
break
}
}
@natecook1000
natecook1000 / fibMemoized
Last active August 29, 2015 14:06
Memoized Fibonnaci series
// this version of memoize is from the WWDC 2014 Advanced Swift session
// https://developer.apple.com/videos/wwdc/2014/?id=404
func memoize<T: Hashable, U>(body: (T -> U, T) -> U ) -> (T) -> U {
var memo = [T: U]()
var result: (T -> U)!
result = {
value in
if let cached = memo[value] { return cached }
@natecook1000
natecook1000 / toUnicodeScalar.swift
Created September 29, 2014 02:46
toUnicodeScalar() method for Character
extension Character {
func toUnicodeScalar() -> UnicodeScalar? {
let s = String(self)
if distance(s.unicodeScalars.startIndex, s.unicodeScalars.endIndex) > 1 {
return nil
}
return s.unicodeScalars[s.unicodeScalars.startIndex]
}
}
@natecook1000
natecook1000 / randomInt.swift
Created October 9, 2014 16:43
Random integers without shuttling between UInt32 and Int
func arc4random_uniform<T: SignedIntegerType>(max: T) -> T {
let max32: Int32 = numericCast(max)
return T(Int64(arc4random_uniform(UInt32(max32))))
}
func arc4random_uniform<T: UnsignedIntegerType>(max: T) -> T {
let max32: UInt32 = numericCast(max)
return T(UInt64(arc4random_uniform(max32)))
}
// see also https://gist.github.com/griotspeak/8bb4c46611fc90d3043b
func findFirst<S: SequenceType>(seq: S, predicate: S.Generator.Element -> Bool) -> S.Generator.Element? {
for x in seq {
if predicate(x) { return x }
}
return nil
}
func not<T>(predicate: T -> Bool) -> (T -> Bool) {
@natecook1000
natecook1000 / ATPView.swift
Created December 9, 2014 19:59
ATP T-shirt in Swift
// adadpted from http://atp.fm/t-shirt
import UIKit
class ATPView : UIView {
override func drawRect(rect: CGRect) {
let fontName = "MyriadPro-SemiBold"
let title = NSLocalizedString("Accidental Tech Podcast", comment: "title")
let initials = NSLocalizedString("ATP", comment: "initials")