Skip to content

Instantly share code, notes, and snippets.

@robnadin
Last active January 8, 2016 15:52
Show Gist options
  • Save robnadin/8d01772cf5d241c7248f to your computer and use it in GitHub Desktop.
Save robnadin/8d01772cf5d241c7248f to your computer and use it in GitHub Desktop.
Custom Swift operators to handle array contents
//
// Array+Operators.swift
//
//
// Created by Rob Nadin on 08/01/2016.
// Copyright © 2016 Rob Nadin. All rights reserved.
//
import Foundation
func <<<T>(inout lhs: [T], rhs: T) {
lhs.append(rhs)
}
func <<<T>(inout lhs: [T], rhs: [T]) {
lhs.appendContentsOf(rhs)
}
// MARK: Optionals
func <<<T>(inout lhs: [T]?, rhs: T?) {
if lhs == nil {
lhs = [T]()
}
if let rhs = rhs {
lhs! << rhs
}
}
func <<<T>(inout lhs: [T]?, rhs: [T]?) {
if lhs == nil {
lhs = [T]()
}
if let rhs = rhs {
lhs! << rhs
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment