This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Adapter design pattern | |
protocol Payment { | |
func recievePayment(_ payment: Double) | |
var totalPayment: Double { get } | |
} | |
class NetBanking: Payment { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
//OperationQueue | |
class RawMaterialImporter: Operation { | |
override func main() { | |
guard !self.isCancelled else { return } | |
print("raw material importing started...") | |
sleep(4) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder