Skip to content

Instantly share code, notes, and snippets.

@staticVoidMan
staticVoidMan / queue.swift
Created September 12, 2023 09:09
Data Structure - Queue
struct QueueFullError<T>: Error {
let value: T
}
struct Queue<T> {
private var elements: [T] = []
var first: T? { elements.first }
var last: T? { elements.last }
var isEmpty: Bool { elements.isEmpty }
/*
Ref:
- https://gist.github.com/rjchatfield/72629b22fa915f72bfddd96a96c541eb
- https://www.hackingwithswift.com/swift/5.4/result-builders
- https://www.avanderlee.com/swift/result-builders
- https://www.swiftbysundell.com/articles/deep-dive-into-swift-function-builders
- https://medium.com/@carson.katri/create-your-first-function-builder-in-5-minutes-b4a717390671
*/
@resultBuilder
struct ArrayBuilder<Element> {
/*
Refs:
- https://swiftrocks.com/understanding-opaque-return-types-in-swift
- https://www.avanderlee.com/swift/some-opaque-types
- https://www.avanderlee.com/swift/existential-any
- https://swiftrocks.com/whats-any-understanding-type-erasure-in-swift
*/
protocol ImageFetching<Image> {
associatedtype Image
@staticVoidMan
staticVoidMan / dispatchQueue.swift
Last active September 22, 2023 05:13
GCD, DispatchQueue, concurrent, barrier
import Foundation
let queue = DispatchQueue(label: "my.queue", attributes: .concurrent)
print("A")
queue.async { // Task1
for index in 0 ..< 5 {
sleep(2)
print("Task 1 - async \(index)")
}
@staticVoidMan
staticVoidMan / getThrows.swift
Last active August 17, 2023 08:07
Computed property with `get` can be marked with `throws`
// Ref: https://www.linkedin.com/posts/ihendi_ios-interview-questions-answers-could-activity-7094859984340951040-iL-x/
struct PersonNameEmpty: Error {}
struct Person {
var name: String = ""
var formattedName: String {
get throws {
guard name.isEmpty == false else { throw PersonNameEmpty() }
@staticVoidMan
staticVoidMan / threadSafe.swift
Created August 16, 2023 12:12
Thread-safe read/write using DispatchQueue & barrier concept
class Test {
private var _name: String = "Hello, World!"
let queue = DispatchQueue(label: "thread.safe", attributes: .concurrent)
var name: String {
get {
queue.sync {
self._name
}
@staticVoidMan
staticVoidMan / actorExample.swift
Created August 16, 2023 08:08
Actor, await, non-isolated keyword
// Ref: https://www.avanderlee.com/swift/actors
actor ChickenFeeder {
let food = "worms"
var numberOfEatingChickens: Int = 0
func chickenStartsEating() {
numberOfEatingChickens += 1
}
@staticVoidMan
staticVoidMan / strategyPattern.swift
Last active August 14, 2023 10:40
Strategy Design Pattern - Encryption
/*
Ref:
- https://springframework.guru/gang-of-four-design-patterns/strategy-pattern
- https://refactoring.guru/design-patterns/strategy/swift/example
*/
import Foundation
protocol MessageEncryptor {
func encrypt(_ value: String) -> String
@staticVoidMan
staticVoidMan / decoratorPattern.swift
Last active August 14, 2023 10:39
Decorator Design Pattern - Retry Management
/*
Ref:
- https://springframework.guru/gang-of-four-design-patterns/decorator-pattern
- https://refactoring.guru/design-patterns/decorator/swift/example
*/
import Foundation
enum DataProviderError: Error {
case unavailable
@staticVoidMan
staticVoidMan / binarySearch.swift
Last active August 7, 2023 07:21
Algo Binary Search using 2 pointer logic
// Ref: https://www.youtube.com/watch?v=V_T5NuccwRA
func find(_ compare: Int, in array: [Int]) -> Int? {
var low = 0
var high = array.count - 1
repeat {
let mid = (low + high)/2
let current = array[mid]
// print(low...high, mid, current, compare)