Skip to content

Instantly share code, notes, and snippets.

View JarvisTheAvenger's full-sized avatar
🎯
Focusing

Rahul Umap JarvisTheAvenger

🎯
Focusing
View GitHub Profile
// Custom Implementation of higher order functions: map, filter, reduce
extension Collection {
func customMap<T>(_ tranform: (Element) -> T) -> [T] {
var result = [T]()
for element in self {
result.append(tranform(element))
}
import Foundation
// Subscript are used to access the information from collection, sequence and a list in classes, structures and enumerations
class DaysOfWeek {
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
subscript(index: Int) -> String {
get {
return days[index]
@JarvisTheAvenger
JarvisTheAvenger / Adapter.swift
Created August 24, 2021 04:24
Adapter pattern
import Foundation
// Adapter design pattern
protocol Payment {
func recievePayment(_ payment: Double)
var totalPayment: Double { get }
}
class NetBanking: Payment {
import Foundation
// Indirect enums are enums that need to reference themselves somehow,
// and are called “indirect” because they modify the way Swift stores them so they can grow to any size.
// Without the indirection, any enum that referenced itself could potentially become infinitely sized:
// it could contain itself again and again, which wouldn’t be possible
indirect enum LinkedListItem<T> {
case endNode(value: T)
case nextNode(value: T, next: LinkedListItem)
import Foundation
// NSRecursiveLock - It is used to write thread safe code
// Locks needs to be balanced.
class Logger {
let recursiveLock = NSRecursiveLock()
func doSomething() {
recursiveLock.lock()
import Foundation
//OperationQueue
class RawMaterialImporter: Operation {
override func main() {
guard !self.isCancelled else { return }
print("raw material importing started...")
sleep(4)
}
import Foundation
// DispatchWorkItem
// DispatchWorkItem API is introduced in iOS 8 to provide more control over dispatch queue
// DispatchWorkItem can be used to cancel the tasks
struct Counter {
func count() {
var countWorkItem : DispatchWorkItem?
import Foundation
// Concurrent queue
// When the order of execution of tasks doesn't matter, you can use concurrent queue.
// Concurrent queue are faster than serial queue.
// Concurrent queue can create race conditions in the code.
// NSLock - Used to avoid the race condition.
var balance = 5000
import Foundation
// Dependency Injection
/*
Dependency Injection is often used with the intention of writing code that is loosely coupled, and thus, easier to test.
Types of dependecy injection:
1. Property Injection
2. Constructor Injection
import Foundation
// Property Wrapper
// A property wrapper adds a layer of separation between code that manages how a property is stored and
// the code that defines a property. For example, if you have properties that provide thread-safety checks or store
// their underlying data in a database, you have to write that code on every property. When you use a property wrapper,
// you write the management code once when you define the wrapper, and then reuse that management code by applying it to multiple properties.
@propertyWrapper