Skip to content

Instantly share code, notes, and snippets.

@0xMarK
0xMarK / EmptyToNil.swift
Created October 20, 2023 08:12
Property wrapper to turn empty collection into nil
import Foundation
@propertyWrapper
struct EmptyToNil<WrappedType: Codable & Collection>: Codable {
let wrappedValue: WrappedType?
init(wrappedValue: WrappedType?) {
self.wrappedValue = wrappedValue
}
@0xMarK
0xMarK / insertionSort.swift
Last active November 30, 2020 09:24
Insertion Sort
func insertionSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else { return array }
var sorted = array
for i in 1..<array.count {
let current = sorted[i]
var next: Int?
for j in 0...i {
if next == nil,
current < sorted[j] {
next = current
@0xMarK
0xMarK / selectionSort.swift
Created November 29, 2020 19:58
Selection Sort
func selectionSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else { return array }
var sorted = array
var smallestIndex = 0
for i in 0..<array.count {
for j in (i + 1)..<array.count {
if sorted[j] < sorted[smallestIndex] {
smallestIndex = j
}
}
@0xMarK
0xMarK / bubbleSort.swift
Last active November 29, 2020 19:33
Bubble Sort
func bubbleSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else { return array }
var sorted = array
var length = array.count
while length > 1 {
for i in 1..<length {
if sorted[i - 1] > sorted[i] {
let current = sorted[i]
sorted[i] = sorted[i - 1]
sorted[i - 1] = current
@0xMarK
0xMarK / reverseString.swift
Created November 29, 2020 18:41
reverseString
func reverseStringRecursive(_ string: String) -> String {
guard !string.isEmpty else { return string }
return reverseStringRecursive(String(string.suffix(string.count - 1))) + String(string[string.index(string.startIndex, offsetBy: 0)])
}
func reverseStringIterative(_ string: String) -> String {
var result = ""
for i in (0..<string.count).reversed() {
result += String(string[string.index(string.startIndex, offsetBy: i)])
}
@0xMarK
0xMarK / fibonacci.swift
Last active November 29, 2020 18:40
Fibonacci
func fibonacciRecursive(_ index: Int) -> Int {
if index < 2 {
return index
}
return fibonacciRecursive(index - 2) + fibonacciRecursive(index - 1)
}
func fibonacciIterative(_ index: Int) -> Int {
if index < 2 {
return index
@0xMarK
0xMarK / factorial.swift
Created November 29, 2020 18:39
Factorial
func factorialRecursive(_ number: Int) -> Int {
if number < 3 {
return number
}
return number * factorialRecursive(number - 1)
}
func factorialIterative(_ number: Int) -> Int {
var result = 1
for i in (2...number) {
@0xMarK
0xMarK / ChatCollectionView.swift
Created July 25, 2019 09:01
ChatCollectionView
class ChatViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
collectionView.transform = CGAffineTransform.reversed
// .........
}
}
@0xMarK
0xMarK / MyDataModel.swift
Created July 15, 2019 14:25
SwifttUI CoreData integration
class MyDataModel: BindableObject {
var didChange = PassthroughSubject<Void, Never>()
}
extension MyDataModel: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
didChange.send()
}
}
@0xMarK
0xMarK / KeyValueObserving.swift
Last active July 15, 2019 14:19
SwiftUI (Combine) Publishers
class MyDefaults: BindableObject {
public var didChange: AnyPublisher<Bool, Never>
init() {
let defaults = UserDefaults.standard
didChange = Publishers.Merge(
defaults.publisher(for: \.userOption1),
defaults.publisher(for: \.userOption2)
)
.receive(on: RunLoop.main)