Skip to content

Instantly share code, notes, and snippets.

View jpmcglone's full-sized avatar

John P. McGlone jpmcglone

View GitHub Profile
@jpmcglone
jpmcglone / Array+Additions.swift
Last active March 14, 2019 17:38
array.filter into multiple arrays in one pass
public struct ConditionalArrayFilter <Element> {
let condition: (Element) -> Bool // the condition to meet
private var _array = Array<Element>()
var array: Array<Element> {
get { return _array }
}
init(condition: (Element) -> Bool) { self.condition = condition }
}
private extension ConditionalArrayFilter {
@jpmcglone
jpmcglone / Array+Additions.swift
Created August 8, 2016 18:43
Array extension: prepend
import Foundation
extension Array {
mutating func prepend(newElement: Element) {
self.insert(newElement, atIndex: 0)
}
}
@jpmcglone
jpmcglone / gist:5a5e866ef333716d6d88b55f93b95e15
Created August 13, 2016 21:58
Remove All Gesture Recognizers
func removeAllGestureRecognizers() {
gestureRecognizers?.forEach(self.removeGestureRecognizer)
}
@jpmcglone
jpmcglone / Realm+Sync
Created September 16, 2016 02:31
Realm 'sync' function (Swift 3)
import RealmSwift
extension Realm {
func sync<T: Object>(_ obj: T) {
var beganWriteTransaction = false
if !isInWriteTransaction {
beganWriteTransaction = true
beginWrite()
}
@jpmcglone
jpmcglone / Additions.swift
Created October 4, 2016 16:56
Swift 3 - Set if not nil
import Foundation
func setIfNotNil<T>(_ obj: inout T?, _ other: T?) {
guard other != nil else { return }
obj = other
}
@propertyWrapper
class Lazy<T> {
fileprivate var loader: (()->T)
fileprivate var _value: T?
var projectedValue: Lazy<T> { return self }
var wrappedValue: T {
_value = _value ?? loader()
return _value!
}
import Foundation
public class BidirectionalMap<Left: Hashable, Right: Hashable> {
private var leftToRightMapping = [Left : Right]()
private var rightToLeftMapping = [Right : Left]()
public subscript(left: Left) -> Right? {
get { return leftToRightMapping[left] }
set (newValue) {
import Foundation
class Weak<T> {
private weak var _value: AnyObject?
var value: T? { return _value as? T }
init(_ value: T) {
_value = value as AnyObject
}
}
func highestKey<T, N: Comparable>(of dictionary: Dictionary<T, N>) -> T? {
var highestKey: T?
var highestValue: N?
for key in dictionary.keys {
let value = dictionary[key]
if highestValue == nil || value! > highestValue! {
highestKey = key
highestValue = value
}
public struct VSpacer: View {
private let spacing: CGFloat
public init(_ spacing: CGFloat) {
self.spacing = spacing
}
public var body: some View {
Spacer()
.frame(idealHeight: spacing, maxHeight: spacing)