Skip to content

Instantly share code, notes, and snippets.

View masters3d's full-sized avatar

masters3d masters3d

  • Pacific North West
View GitHub Profile
@austinzheng
austinzheng / main.swift
Last active March 7, 2016 20:41
Anonymous class experiment in Swift
import Foundation
protocol SomeDelegateProtocol : class {
func firstFunc() -> String
func secondFunc() -> Bool
func thirdFunc() -> Self
}
class MyClass {
weak var delegate : SomeDelegateProtocol?
@erica
erica / Euler Challenge
Created July 30, 2014 20:13
Explore Swift Challenge #1
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
@austinzheng
austinzheng / array_structure.swift
Last active August 1, 2018 07:04
A horrendous example of custom pattern matching in Swift
// 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)
@sketchytech
sketchytech / Common Swift String Extensions
Last active February 3, 2021 19:03 — forked from albertbori/Common Swift String Extensions
Added and amended to use pure Swift where possible
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 {
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> {
@rjchatfield
rjchatfield / linkedList.swift
Created August 16, 2014 12:21
LinkedList in Swift
// LINKED LIST
/**
* NODE
*/
private class Node<T:protocol<Printable, Comparable>>: Printable, Comparable {
/**
* PROPERTIES
*/
var info:T
@sketchytech
sketchytech / Calculating a count for UTF16 string with unicode scalar characters - use utf16Count
Last active September 17, 2015 20:58
Working with bytes and strings in Swift (Unicode Scalar, UTF8 and UTF16)
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) {
@krzyzanowskim
krzyzanowskim / variable_with_class_constant_length
Last active August 29, 2015 14:06
How to use class constant to initialize variable - solution is use lazy variable
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] = {
@krzyzanowskim
krzyzanowskim / shift_left_generic
Last active August 29, 2015 14:06
Shift bits to the left - Generic version. This is the most advanced Generic function I ever wrote (req: extensions, protocol, funcs)
// 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)
}
@Brimizer
Brimizer / swift_range_convert
Created September 8, 2014 06:44
Swift Convert Range<Int> to Range<String.Index>
// 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 {