Code Monkey home page Code Monkey logo

foundationoperators's Introduction

#FoundationOperators

Operator overloading is fun. It's also easy to get carried away.

But this is a project for hackathon, so that's exactly what we did.

We overloaded the heck out of operators, and also extended Int.

Int extension

We created very creatively named protocol DateComponentsConvertible and provided implementation of it for Int.

DateComponentsConvertible is defined as:

import Foundation

protocol DateComponentsConvertible {
    var years: NSDateComponents { get }
    var months: NSDateComponents { get }
    var days: NSDateComponents { get }
    var hours: NSDateComponents { get }
    var minutes: NSDateComponents { get }
    var seconds: NSDateComponents { get }
}

...and pretty horribly implemented like this:

extension Int: DateComponentsConvertible {
    var years: NSDateComponents {
        let component = NSDateComponents()
        component.year = self
        return component
    }

    var months: NSDateComponents {
        let component = NSDateComponents()
        component.month = self
        return component
    }

    var days: NSDateComponents {
        let component = NSDateComponents()
        component.day = self
        return component
    }

    var hours: NSDateComponents {
        let component = NSDateComponents()
        component.hour = self
        return component
    }

    var minutes: NSDateComponents {
        let component = NSDateComponents()
        component.day = self
        return component
    }

    var seconds: NSDateComponents {
        let component = NSDateComponents()
        component.day = self
        return component
    }
}

Which allows you to do cool stuff like this:

let quarter = 15.minutes
let workday = 8.hours
let timeSinceiPhoneIntroduction = 7.years

Overloaded operators

We also overloaded a bunch of operators to help (and/or confuse) you while working with Foundation types.

NSArray

+ operator

Defined as:

@infix func + (lhs: NSArray, rhs: NSObject) -> NSArray {
    return lhs.arrayByAddingObject(rhs)
}

Returns NSArray from the left hand side, with NSObject from the right hand side appended at the end.

Not to be confused with ++ operator used to merge two arrays.

let randomObjects = [1, 2, 5.0, "something"]
let now = NSDate()

randomObjects + now //

+= operator

Definition:

@assignment func += (inout array: NSArray, object: NSObject) {
    array = array + object
}

Like +, but with 100% more assignment.

- operator

Definition:

@infix func - (array: NSArray, object: NSObject) -> NSArray {
    let mutableArray = NSMutableArray(array: array)
    mutableArray.removeObject(object)
    return NSArray(array: mutableArray)
}

Returns array from left hand side, with object from the right hand side removed.

let array = ["foo", "bar", now]

let foobar = array - now //["foo", "bar"]

-= operator

Definition:

@assignment func -= (inout array: NSArray, object: NSObject) {
    array = array - object
}

Like -,but assigns.

++ operator

Definition:

operator infix ++ {}
@infix func ++ (left: NSArray, right: NSArray) -> NSArray {
    return left.arrayByAddingObjectsFromArray(right)
}

Example:

let names: NSArray = ["tommy", "lee", "jones"]
let movies: NSArray = ["fugitive", "man_in_black"]

names + movies // ["tommy", "lee", "jones", "fugitive", "man_in_black"]

++= operator

Definition:

operator infix ++= {}
@assignment func ++= (inout left: NSArray, right: NSArray) {
    left = left ++ right
}

Like ++, but assigns.

-- operator

Removes objects contained in right hand side array from array on the left hand side.

operator infix -- {}
@infix func -- (left: NSArray, right: NSArray) -> NSArray {
    var resultArray = NSMutableArray(array: left)
    for element:AnyObject in right {
        resultArray.removeObject(element)
    }
    return NSArray(array: resultArray)
}

Example:

let tommyLeeJones: NSArray = ["tommy", "lee", "jones"]
let leeJones: NSArray = ["lee", "jones"]

tommyLeeJones -- leeJones // ["tommy"]

--= operator

Like --, but assigns.

foundationoperators's People

Contributors

jklausa avatar jednymslowem avatar robbartoszewski avatar ashfurrow avatar krzyzanowskim avatar

Watchers

Maciej Gad avatar James Cloos avatar Krzysztof Rodak avatar Marek Kotewicz avatar Adam Borek avatar k.rzeznicki avatar Wojciech Stasiński avatar Michał Mynarski avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.