Skip to content

Instantly share code, notes, and snippets.

@Nirajpaul2
Nirajpaul2 / CompressionStrategy.swift
Last active December 1, 2024 09:44
CompressionStrategy
// Step 1: Define the Compression Strategy Protocol
// This protocol ensures all compression strategies have a common interface.
protocol CompressionStrategy {
func compress(files: [String]) -> String
}
// Step 2: Concrete Compression Strategies
// ZIP compression strategy
class ZipCompression: CompressionStrategy {
@Nirajpaul2
Nirajpaul2 / Validation.swift
Created December 1, 2024 09:41
Validation
// Step 1: Define the Validation Strategy Protocol
// Each validation strategy will implement this protocol.
protocol ValidationStrategy {
func validate(_ input: String) -> Bool
}
// Step 2: Concrete Validation Strategies
// Email validation strategy
class EmailValidation: ValidationStrategy {
@Nirajpaul2
Nirajpaul2 / SortingStrategy.swift
Last active December 1, 2024 09:36
SortingStrategy
// Define a protocol that provides a common interface for sorting strategies
protocol SortingStrategy {
func sort(_ numbers: inout [Int]) // Method to sort an array of integers
}
// Concrete implementation of the SortingStrategy using Bubble Sort
class BubbleSort: SortingStrategy {
func sort(_ numbers: inout [Int]) {
numbers.sort(by: <) // Sorting in ascending order (using a simple built-in sort for demonstration)
print("Sorting array using bubble sort strategy")
@Nirajpaul2
Nirajpaul2 / PaymentService.swift
Created December 1, 2024 09:33
PaymentService
// Define a protocol that all payment methods must conform to
// It ensures a uniform interface for calculating the processing fee
protocol PaymentMethodProtocol {
func calculateProcessingFee(for amount: Double) -> Double
}
// Concrete implementation of the PaymentMethodProtocol for Credit Card payments
class CreditCardPayment: PaymentMethodProtocol {
func calculateProcessingFee(for amount: Double) -> Double {
return amount * 0.02 // 2% fee for credit card payments
@Nirajpaul2
Nirajpaul2 / family of algorithms.swift
Last active December 1, 2024 09:10
family of algorithms
//************************************** family of algorithms ******************************
A family of algorithms refers to a set of related algorithms that solve the same type of problem but use different approaches.
For example:
Sorting: Bubble Sort, Quick Sort, Merge Sort.
Payment processing: Credit Card, Debit Card, Net Banking.
Compression: ZIP, RAR, GZIP.
These algorithms share a common purpose or goal but differ in their implementation details.
@Nirajpaul2
Nirajpaul2 / LazyGridView.swift
Created August 14, 2022 07:12
LazyGridViewHorizontal
ScrollView(.horizontal) {
       LazyHGrid(rows: threeColumnGrid) {
           // Display the item
       }
  }
@Nirajpaul2
Nirajpaul2 / LazyGridView.swift
Last active August 14, 2022 07:09
LazyGridViewThreeColumn
ScrollView {
      LazyVGrid(columns: threeColumnGrid) {
          // Display the item
      }
 }
@Nirajpaul2
Nirajpaul2 / AtomicWrapper.swift
Last active April 15, 2022 04:56
AtomicWrapper
struct Atomic<Value> {
private let queue = DispatchQueue(label: "com.vadimbulavin.atomic")
private var value: Value
init(wrappedValue: Value) {
self.value = wrappedValue
}
var wrappedValue: Value {
@Nirajpaul2
Nirajpaul2 / UserDefaultWrapper.swift
Last active April 15, 2022 04:56
UserDefaultWrapper
import Foundation
import Combine
@propertyWrapper
struct UserDefaultWrapper<Value: Codable> {
let key: String
let defaultValue: Value
var container: UserDefaults = .standard
private let publisher = PassthroughSubject<Value, Never>()
@Nirajpaul2
Nirajpaul2 / CapitalizedWrapper.swift
Last active April 15, 2022 04:57
CapitalizedWrapper
@propertyWrapper
struct Capitalized {
var wrappedValue: String {
didSet { wrappedValue = wrappedValue.capitalized }
}
init(wrappedValue: String) {
self.wrappedValue = wrappedValue.capitalized
}
}