Skip to content

Instantly share code, notes, and snippets.

View masters3d's full-sized avatar

masters3d masters3d

  • Pacific North West
View GitHub Profile
@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) {
@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
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> {
@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 {
@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)
@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 / main.swift
Last active April 30, 2026 00:10
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?
@kareman
kareman / Queue.swift
Last active July 31, 2020 19:16
A standard queue (FIFO - First In First Out) implemented in Swift. Supports simultaneous adding and removing, but only one item can be added at a time, and only one item can be removed at a time. Using the "Two-Lock Concurrent Queue Algorithm" from http://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html#tlq, without the locks.
//
// Queue.swift
// NTBSwift
//
// Created by Kåre Morstøl on 11/07/14.
//
// Using the "Two-Lock Concurrent Queue Algorithm" from http://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html#tlq, without the locks.
// should be an inner class of Queue, but inner classes and generics crash the compiler, SourceKit (repeatedly) and occasionally XCode.
import Darwin
import CoreGraphics
protocol ScalarFunctions {
var acos:Double {get}
var asin:Double {get}
var atan:Double {get}
func atan2(x:Double) -> Double
var cos:Double {get}
var sin:Double {get}
@ariok
ariok / [swift]ArrayExtension.swift
Created June 27, 2014 14:56
[swift]Adding support for "indexOf:" and "contains:" to Array
extension Array {
func contains(object:AnyObject) -> Bool {
return self.bridgeToObjectiveC().containsObject(object)
}
func indexOf(object:AnyObject) -> Int {
return self.bridgeToObjectiveC().indexOfObject(object)
}
}