Skip to content

Instantly share code, notes, and snippets.

@mikeger
Created March 1, 2016 09:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeger/e7c3d664dddd7322977c to your computer and use it in GitHub Desktop.
Save mikeger/e7c3d664dddd7322977c to your computer and use it in GitHub Desktop.
Swift custom operators
import Foundation
import UIKit
extension String {
var attributedString: NSAttributedString {
return NSAttributedString(string: self)
}
}
// Concats the lhs and rhs and returns a NSAttributedString
infix operator + { associativity left precedence 140 }
func +(left: NSAttributedString, right: NSAttributedString) -> NSAttributedString {
let result = NSMutableAttributedString()
result.appendAttributedString(left)
result.appendAttributedString(right)
return result
}
func +(left: String, right: NSAttributedString) -> NSAttributedString {
var range : NSRange? = NSMakeRange(0, 0)
let attributes = right.length > 0 ? right.attributesAtIndex(0, effectiveRange: &range!) : [:]
let result = NSMutableAttributedString()
result.appendAttributedString(NSAttributedString(string: left, attributes: attributes))
result.appendAttributedString(right)
return result
}
func +(left: NSAttributedString, right: String) -> NSAttributedString {
var range : NSRange? = NSMakeRange(0, 0)
let attributes = left.length > 0 ? left.attributesAtIndex(left.length - 1, effectiveRange: &range!) : [:]
let result = NSMutableAttributedString()
result.appendAttributedString(left)
result.appendAttributedString(NSAttributedString(string:right, attributes: attributes))
return result
}
// Concats the lhs and rhs and assigns the result to the lhs
infix operator += { associativity right precedence 90 }
func +=(inout left: NSMutableAttributedString, right: String) -> NSMutableAttributedString {
left.appendAttributedString(right.attributedString)
return left
}
func +=(inout left: NSAttributedString, right: String) -> NSAttributedString {
left = left + right
return left
}
func +=(inout left: NSAttributedString, right: NSAttributedString) -> NSAttributedString {
left = left + right
return left
}
func +=(inout left: NSAttributedString, right: NSAttributedString?) -> NSAttributedString {
guard let rhs = right else { return left }
return left += rhs
}
// Applies the attributes on the rhs to the string on the lhs
infix operator && { associativity left precedence 150 }
func &&(left: String, right: [String: AnyObject]) -> NSAttributedString {
let result = NSAttributedString(string: left, attributes: right)
return result
}
func &&(left: String, right: UIFont) -> NSAttributedString {
let result = NSAttributedString(string: left, attributes: [NSFontAttributeName: right])
return result
}
func &&(left: String, right: UIColor) -> NSAttributedString {
let result = NSAttributedString(string: left, attributes: [NSForegroundColorAttributeName: right])
return result
}
func &&(left: NSAttributedString, right: UIColor) -> NSAttributedString {
let result = NSMutableAttributedString(attributedString: left)
result.addAttributes([NSForegroundColorAttributeName: right], range: NSMakeRange(0, result.length))
return result
}
func &&(left: NSAttributedString, right: [String: AnyObject]) -> NSAttributedString {
let result = NSMutableAttributedString(attributedString: left)
result.addAttributes(right, range: NSMakeRange(0, result.length))
return result
}
import Foundation
// forward pipe
infix operator |> { associativity left precedence 80}
func |> <T, U>(value: T, function: (T -> U)) -> U {
return function(value)
}
let result = value |> func1 |> func2 |> func1
// function composition
infix operator >> { associativity left }
func >> <T1, T2, T3> (left: (T1)->T2, right: (T2)->T3) -> (T1)->T3 {
return { (t1: T1) -> T3 in return right(left(t1)) }
}
// Experiment: times operator
infix operator × { associativity left precedence 100 }
func × <A> (times: UInt, operation: (UInt) -> A) -> [A] {
var result: [A] = [A]()
for i in 1...times {
result.append(operation(i))
}
return result
}
let str = "Hello, playground"
postfix operator ^^ {}
postfix func ^^ (value: String) -> String {
return value.uppercaseString
}
str^^
import Foundation
infix operator × { associativity left precedence 100 }
func × <A> (times: UInt, operation: (UInt) -> A) -> [A] {
var result: [A] = [A]()
for i in 1...times {
result.append(operation(i))
}
return result
}
extension Array {
func randomItem() -> Element {
let index = Int(arc4random_uniform(UInt32(self.count)))
return self[index]
}
}
enum Sex: Int {
case Male = 0
case Female = 1
static func anySex() -> Sex {
return Sex(rawValue: Int(arc4random_uniform(UInt32(2))))!
}
}
struct 🐱 {
let name: String
let sex: Sex
init(name: String, sex: Sex) {
self.name = name
self.sex = sex
}
static func anyName(sex: Sex) -> String {
let namesMales = ["Bobbie", "Tommie", "Shadow", "Kitty", "Jasper"]
let namesFemales = ["Shadow", "Kitty", "Jasper", "Lucy"]
switch (sex) {
case .Male:
return namesMales.randomItem()
case .Female:
return namesFemales.randomItem()
}
}
static func anyCat() -> 🐱 {
let newSex = Sex.anySex()
let new🐱 = 🐱(name: 🐱.anyName(newSex), sex: newSex)
return new🐱
}
}
infix operator ❤️ { associativity left precedence 100 }
func ❤️ (a: 🐱, b: 🐱) -> [🐱] {
let amount = UInt(arc4random_uniform(UInt32(5)))
return amount × { _ in return 🐱.anyCat() }
}
let 😺 = 🐱(name: "Bob", sex: .Male)
let 😻 = 🐱(name: "Lucy", sex: .Female)
let cats = 😺 ❤️ 😻
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment