Skip to content

Instantly share code, notes, and snippets.

View dagostini's full-sized avatar

Dejan Agostini dagostini

View GitHub Profile
@dagostini
dagostini / CreateDispatchSource.swift
Created August 2, 2017 22:27
Creating a dispatch source.
guard fileExists() == true else {
return
}
let descriptor = open(self.filePath, O_EVTONLY)
if descriptor == -1 {
return
}
self.eventSource = DispatchSource.makeFileSystemObjectSource(fileDescriptor: descriptor, eventMask: self.fileSystemEvent, queue: self.dispatchQueue)
@dagostini
dagostini / Testing+MyOtherProtocol.swift
Created June 4, 2017 22:04
Generics Testing Class Example
protocol MyOtherProtocol {
associatedtype ItemType
mutating func append(_ item: ItemType)
var count: Int { get }
subscript(i: Int) -> ItemType { get }
}
class Testing: MyOtherProtocol {
typealias ItemType = Int
@dagostini
dagostini / Node+Equatable.swift
Created June 4, 2017 22:01
Generics Node Equatable Example
extension Node where T: Equatable {
func printItemTitle(item: T, secondItem: T) -> String {
if item == secondItem {
return "equal"
} else {
return item.itemTitle
}
}
}
@dagostini
dagostini / Node+MyCustomProtocol.swift
Created June 4, 2017 22:00
Generics Node Custom Protocol Example
protocol MyCustomProtocol {
var itemTitle: String { get }
}
class Node<T: MyCustomProtocol> {
var item: T
var next: Node?
init(withItem item: T) {
self.item = item
@dagostini
dagostini / DASelectionSort_Generics.swift
Created June 4, 2017 21:59
Generics DASelectionSort Example
class DASelectionSort<T: Comparable> {
public static func sort(_ items: [T]) -> [T] {
var result = items
let length = result.count
for i in 0..<length {
var minIndex = i
for j in i+1..<length {
@dagostini
dagostini / Node+Extension.swift
Created June 4, 2017 21:57
Generics Node Extension
extension Node {
func processItem(item: T) {
// Access type parameter in the extension
}
}
@dagostini
dagostini / Node.swift
Created June 4, 2017 21:56
Generics Node.swift
class Node<T> {
var item: T
var next: Node?
init(withItem item: T) {
self.item = item
}
}
func genericFunction<Item>(item: Item, secondItem: Item) {
// do something useful with these two items :)
}
// Integers as parameters
genericFunction(item: 2, secondItem: 3)
// Strings as parameters
genericFunction(item: "first", secondItem: "second")
@dagostini
dagostini / DAQuickSort.swift
Created April 25, 2017 21:13
Quick Sort swift imlementation.
class DAQuickSort<T: Comparable> {
public static func sort(_ items: [T]) -> [T] {
var result = items
result.shuffle()
sort(original: &result, low: 0, high: result.count - 1)
return result
}
@dagostini
dagostini / DAMergeSort.swift
Created April 25, 2017 21:11
Swift implementation of the merge sort.
class DAMergeSort<T: Comparable> {
public static func sort(_ items: [T]) -> [T] {
var result = items
var temp = result
sort(original: &result, temp: &temp, low: 0, high: result.count - 1)
return result
}