This file contains hidden or 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 Foundation | |
protocol SomeDelegateProtocol : class { | |
func firstFunc() -> String | |
func secondFunc() -> Bool | |
func thirdFunc() -> Self | |
} | |
class MyClass { | |
weak var delegate : SomeDelegateProtocol? |
This file contains hidden or 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
struct PrimeGenerator : Generator { | |
typealias Element = Int | |
private var myPrimes = [Int]() // store previous primes | |
var current : Int = 2 // first prime is 2 | |
mutating func next() -> Int? { | |
if myPrimes.count == 0 {myPrimes = [2]; return 2} | |
NextNumber: while (current++ > 0) { | |
for prime in myPrimes { | |
// If a number has a prime factor it is not prime |
This file contains hidden or 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 Cocoa | |
enum Wildcard : NilLiteralConvertible { | |
case Single | |
case FromBeginning | |
case ToEnd | |
case Range(Int) | |
case Literal(Int) |
This file contains hidden or 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 Foundation | |
extension String | |
{ | |
// Works in Xcode but not Playgrounds because of a bug with .insert() | |
mutating func insertString(string:String,ind:Int) { | |
var insertIndex = advance(self.startIndex, ind, self.endIndex) | |
for c in string { |
This file contains hidden or 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
struct TakeSequenceView<Base : SequenceType> : SequenceType { | |
let base: Base | |
let n: Int | |
init(_ base: Base, n: Int) { | |
self.base = base | |
self.n = n | |
} | |
func generate() -> GeneratorOf<Base.Generator.Element> { |
This file contains hidden or 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
// LINKED LIST | |
/** | |
* NODE | |
*/ | |
private class Node<T:protocol<Printable, Comparable>>: Printable, Comparable { | |
/** | |
* PROPERTIES | |
*/ | |
var info:T |
This file contains hidden or 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
newStr = "hello 😂😒😡" | |
// the number of elements: | |
let count = newStr.utf16Count | |
// create array of appropriate length: | |
var array = [UInt16](count: count, repeatedValue: 0) | |
for a in enumerate(newStr.utf16) { |
This file contains hidden or 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 Foundation | |
class Foo { | |
let length = 255 | |
// !!! not like this because self.length can't be used here | |
// var arrayConstantLength = [Byte](count:self.length, repeatedValue:0) | |
// but like this (with lazy I can use self.length constant) | |
lazy var arrayConstantLength:[Byte] = { |
This file contains hidden or 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 Foundation | |
/** Protocol and extensions for integerFromBitsArray. Bit hakish for me, but I can't do it in any other way */ | |
protocol Initiable { | |
init(_ v: Int) | |
init(_ v: UInt) | |
} |
This file contains hidden or 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
// Public domain. | |
// Free to use. | |
// Use like this: | |
var world = "Hello, world!" | |
let convertedRange = world.convertRange(0..<5) | |
world.removeRange(convertedRange) | |
// Converts a regular range (0..5) to a proper String.Index range. | |
extension String { |