Skip to content

Instantly share code, notes, and snippets.

View ahcodedev's full-sized avatar
🏠
Working from home

Aaron ahcodedev

🏠
Working from home
  • United States
View GitHub Profile
@ahcodedev
ahcodedev / swift-zip.swift
Last active April 17, 2017 22:54
Swift Zip function usage
//Zip function (Sequence)
// https://github.com/apple/swift/blob/master/stdlib/public/core/Zip.swift
// https://developer.apple.com/reference/swift/1541125-zip
// http://swiftdoc.org/v3.0/func/zip/
let array1 = ["one", "two", "three", "four", "five"]
let array2 = 1...5
let zipSequence = zip(array1, array2)
type(of: zipSequence) //Zip2Sequence<Array<String>, CountableClosedRange<Int>>.Type
@ahcodedev
ahcodedev / swift-custom-maps.swift
Last active April 17, 2017 22:54
Custom Maps in Swift
//Custom Map functions
//Experimenting with making custom map functions
//Original Map implementation: https://github.com/apple/swift/blob/master/stdlib/public/core/Sequence.swift
extension Sequence {
func mapToDictionary<T>(_ transform: (Iterator.Element) throws -> (String, T)) rethrows -> [String: T] {
let initialCapacity = underestimatedCount
var result = [String: T]()
var iterator = self.makeIterator()
@ahcodedev
ahcodedev / bridge-pattern.swift
Last active August 19, 2020 20:42
Example of the bridge pattern in Swift
// Bridge pattern (Structural)
//
// Decouples an abstraction from its implementation, so that the two can vary independently.
// Accomplished by seperating two interacting features and then creating a bridge type to
// handle where they overlap
protocol Database {
func closeConnection()
func openConnection()
func saveRecord(recordToSave: Record)
}
@ahcodedev
ahcodedev / factory-pattern.swift
Last active April 17, 2017 22:56
Example of the factory pattern in Swift
//Factory Pattern (Creational)
//Allows logic of which type will be created to be handled by a method
protocol SimpleType {
var simpleName: String { get }
}
struct Type1: SimpleType {
var simpleName: String = "Type1"
}
@ahcodedev
ahcodedev / builder-pattern.swift
Last active April 17, 2017 22:59
Example of the builder pattern in Swift with protocols
//Playground friendly
import Foundation
/*
The Builder Pattern helps in the creation of complex objects
*Implemented with Protocols
*/
/* Before Builder Pattern is implemented */
@ahcodedev
ahcodedev / singleton-pattern.swift
Last active March 3, 2021 06:44
Example of the singleton pattern in Swift
//XCode Playground Friendly
/**
Singleton Pattern - Ensures that only one instance of a object exists in the application context
usefull for things that will have a global context across the app.
Ex: An internet connection, Credentials, etc.
* Can only be used for reference types.
*/