             return theResult
         } else {
             return nil
+        }
+    }
+}
+
+extension Array {
+
+    mutating func appendAsQueueWithLength(newElement: Element, length: Int) {
+        if self.count < length {
+            self.append(newElement)
+        } else {
+            self.removeFirst()
+            self.append(newElement)
+        }
+        assert(self.count <= length)
+    }
+}
+
+// Calculates the average value of all elements in array
+extension _ArrayType where Generator.Element == Double {
+
+    func getAverage() -> Double {
+        var average: Double = 0.0
+        self.forEach({average += $0})
+        return average / Double(self.count)
+    }
+}
+
+extension CLLocationManager {
+    func enableBackgroundLocation() {
+        if #available(iOS 9.0, *) {
+            self.allowsBackgroundLocationUpdates = true
+        } else {
+            // background location enabled by default
+        }
+    }
+}
+
+extension UITextField {
+
+    func applyAttributedPlaceHolderForTextField() {
+        let attributedPlaceholder = NSMutableAttributedString()
+        let attributedText = NSMutableAttributedString(string: self.placeholder ?? "", attributes: [NSForegroundColorAttributeName : UIColor.lightGrayColor()])
+        attributedPlaceholder.appendAttributedString(attributedText)
+        self.attributedPlaceholder = attributedPlaceholder
+    }
+}
+
+// Extension to get last n elements of array
+extension CollectionType {
+    func last(count:Int) -> [Self.Generator.Element] {
+        let selfCount = self.count as! Int
+        if selfCount <= count - 1 {
+            return Array(self)
+        } else {
+            return Array(self.reverse()[0...count - 1].reverse())
         }
     }
 }

        return String(format: "%d%@", self, suffix)
    }
}


    func addCenterYConstraintsToView(view: UIView, offset: CGFloat) {
        self.addConstraint(NSLayoutConstraint(item: self, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1.0, constant: -offset))
    }

    // pin to all subviews

    func pinSubviewToAllEdges(subview: UIView) {

        subview.translatesAutoresizingMaskIntoConstraints = false

        let left = NSLayoutConstraint(item: self, attribute: .Left, relatedBy: .Equal, toItem: subview, attribute: .Left, multiplier: 1.0, constant: 0.0)
        let right = NSLayoutConstraint(item: self, attribute: .Right, relatedBy: .Equal, toItem: subview, attribute: .Right, multiplier: 1.0, constant: 0.0)
        let top = NSLayoutConstraint(item: self, attribute: .Top, relatedBy: .Equal, toItem: subview, attribute: .Top, multiplier: 1.0, constant: 0.0)
        let bottom = NSLayoutConstraint(item: self, attribute: .Bottom, relatedBy: .Equal, toItem: subview, attribute: .Bottom, multiplier: 1.0, constant: 0.0)

        self.addConstraints([left, right, top, bottom])
    }

    // Height and Width
