Skip to content

Commit

Permalink
Merge pull request #127 from ankurp/for-each
Browse files Browse the repository at this point in the history
Adding each to dollar and cent and corresponding tests
  • Loading branch information
ankurp committed Jan 3, 2015
2 parents 2b4ee54 + 6ae962f commit 662fb3e
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 37 deletions.
42 changes: 31 additions & 11 deletions Cent/Cent/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,33 @@ internal extension Array {
$.cycle(self, callback: callback)
}

/// For each item in the array invoke the callback by passing the elem
///
/// :param callback The callback function to invoke that take an element
func eachWithIndex(callback: (Int, Element) -> ()) -> [Element] {
for (index, elem) in enumerate(self) {
callback(index, elem)
}
return self
}

/// For each item in the array invoke the callback by passing the elem along with the index
///
/// :param callback The callback function to invoke
func each(callback: (Element) -> ()) -> [Element] {
self.eachWithIndex { (index, elem) -> () in
callback(elem)
}
return self
}

/// Checks if the given callback returns true value for all items in the array.
///
/// :param array The array to check.
/// :param iterator Check whether element value is true or false.
/// :param callback Check whether element value is true or false.
/// :return First element from the array.
func every(iterator: (Element) -> Bool) -> Bool {
return $.every(self, iterator: iterator)
func every(callback: (Element) -> Bool) -> Bool {
return $.every(self, callback: callback)
}

/// Get element from an array at the given index which can be negative
Expand All @@ -59,20 +79,20 @@ internal extension Array {
/// that passes the callback check.
///
/// :param array The array to search for the element in.
/// :param iterator Function used to figure out whether element is the same.
/// :return First element's index from the array found using the iterator.
func findIndex(iterator: (Element) -> Bool) -> Int? {
return $.findIndex(self, iterator: iterator)
/// :param callback Function used to figure out whether element is the same.
/// :return First element's index from the array found using the callback.
func findIndex(callback: (Element) -> Bool) -> Int? {
return $.findIndex(self, callback: callback)
}

/// This method is like findIndex except that it iterates over elements of the array
/// from right to left.
///
/// :param array The array to search for the element in.
/// :param iterator Function used to figure out whether element is the same.
/// :return Last element's index from the array found using the iterator.
func findLastIndex(iterator: (Element) -> Bool) -> Int? {
return $.findLastIndex(self, iterator: iterator)
/// :param callback Function used to figure out whether element is the same.
/// :return Last element's index from the array found using the callback.
func findLastIndex(callback: (Element) -> Bool) -> Int? {
return $.findLastIndex(self, callback: callback)
}

/// Gets the first element in the array.
Expand Down
6 changes: 6 additions & 0 deletions Cent/CentTests/CentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ class CentTests: XCTestCase {
XCTAssertEqual("Dollar and Cent" =~ "and Cent\\s+", false, "Should pattern match with regex string")
}

func testEach() {
var arr: [String] = []
XCTAssertEqual(["A", "B", "C"].each({ arr.append($0) }), ["A", "B", "C"], "Return array itself")
XCTAssertEqual(Swift.join("", arr), "ABC", "Return string concatenated")
}

}
60 changes: 42 additions & 18 deletions Dollar/Dollar/Dollar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,39 @@ public class $ {
}
return result
}

/// Call the callback passing each element in the array
///
/// :param array The array to iterate over
/// :param callback function that gets called with each item in the array
/// :return The array passed
public class func each<T>(array: [T], callback: (T) -> ()) -> [T] {
for elem in array {
callback(elem)
}
return array
}

/// Call the callback passing index of the element and each element in the array
///
/// :param array The array to iterate over
/// :param callback function that gets called with each item in the array with its index
/// :return The array passed
public class func each<T>(array: [T], callback: (Int, T) -> ()) -> [T] {
for (index, elem : T) in enumerate(array) {
callback(index, elem)
}
return array
}

/// Checks if the given callback returns true value for all items in the array.
///
/// :param array The array to check.
/// :param iterator Check whether element value is true or false.
/// :param callback Check whether element value is true or false.
/// :return First element from the array.
public class func every<T>(array: [T], iterator: (T) -> Bool) -> Bool {
public class func every<T>(array: [T], callback: (T) -> Bool) -> Bool {
for elem in array {
if !iterator(elem) {
if !callback(elem) {
return false
}
}
Expand Down Expand Up @@ -253,11 +277,11 @@ public class $ {
/// that the callback returns true for.
///
/// :param array The array to search for the element in.
/// :param iterator The iterator function to tell whether element is found.
/// :param callback The callback function to tell whether element is found.
/// :return Optional containing either found element or nil.
public class func find<T>(array: [T], iterator: (T) -> Bool) -> T? {
public class func find<T>(array: [T], callback: (T) -> Bool) -> T? {
for elem in array {
let result = iterator(elem)
let result = callback(elem)
if result {
return elem
}
Expand All @@ -269,11 +293,11 @@ public class $ {
/// that passes the callback check.
///
/// :param array The array to search for the element in.
/// :param iterator Function used to figure out whether element is the same.
/// :return First element's index from the array found using the iterator.
public class func findIndex<T>(array: [T], iterator: (T) -> Bool) -> Int? {
/// :param callback Function used to figure out whether element is the same.
/// :return First element's index from the array found using the callback.
public class func findIndex<T>(array: [T], callback: (T) -> Bool) -> Int? {
for (index, elem : T) in enumerate(array) {
if iterator(elem) {
if callback(elem) {
return index
}
}
Expand All @@ -284,14 +308,14 @@ public class $ {
/// from right to left.
///
/// :param array The array to search for the element in.
/// :param iterator Function used to figure out whether element is the same.
/// :return Last element's index from the array found using the iterator.
public class func findLastIndex<T>(array: [T], iterator: (T) -> Bool) -> Int? {
/// :param callback Function used to figure out whether element is the same.
/// :return Last element's index from the array found using the callback.
public class func findLastIndex<T>(array: [T], callback: (T) -> Bool) -> Int? {
let count = array.count
for (index, _) in enumerate(array) {
let reverseIndex = count - (index + 1)
let elem : T = array[reverseIndex]
if iterator(elem) {
if callback(elem) {
return reverseIndex
}
}
Expand Down Expand Up @@ -866,10 +890,10 @@ public class $ {
/// Removes all elements from an array that the callback returns true.
///
/// :param array The array to wrap.
/// :param iterator Remove elements for which iterator returns true.
/// :param callback Remove elements for which callback returns true.
/// :return Array with elements filtered out.
public class func remove<T>(array: [T], iterator: (T) -> Bool) -> [T] {
return array.filter { !iterator($0) }
public class func remove<T>(array: [T], callback: (T) -> Bool) -> [T] {
return array.filter { !callback($0) }
}

/// The opposite of initial this method gets all but the first element or first n elements of an array.
Expand Down Expand Up @@ -1248,7 +1272,7 @@ public class Chain<C> {
/// :param function Function to tell whether element value is true or false.
/// :return Whether all elements are true according to func function.
public func all(function: (C) -> Bool) -> Bool {
return $.every(self.value, iterator: function)
return $.every(self.value, callback: function)
}

/// Returns if any element in array is true based on the passed function.
Expand Down
12 changes: 9 additions & 3 deletions Dollar/DollarTests/DollarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class DollarTests: XCTestCase {
XCTAssert($.compact([3, nil, 4, 5]) == [3, 4, 5], "Return truth array")
XCTAssertEqual($.compact([nil, nil]) as [NSObject], [], "Return truth array")
}

func testEach() {
var arr: [Int] = []
XCTAssert($.each([1, 3, 4, 5], { arr.append($0 * 2) }) == [1, 3, 4, 5], "Return the array itself")
XCTAssert(arr == [2, 6, 8, 10], "Return array with doubled numbers")
}

func testFlatten() {
XCTAssertEqual($.flatten([[3], 4, 5]), [3, 4, 5], "Return flat array")
Expand Down Expand Up @@ -122,7 +128,7 @@ class DollarTests: XCTestCase {
}

func testRemove() {
XCTAssertEqual($.remove([1, 2, 3, 4, 5, 6], iterator: { $0 == 2 || $0 == 3 }), [1, 4, 5, 6], "Remove based on callback")
XCTAssertEqual($.remove([1, 2, 3, 4, 5, 6], callback: { $0 == 2 || $0 == 3 }), [1, 4, 5, 6], "Remove based on callback")
}

func testSortedIndex() {
Expand Down Expand Up @@ -186,8 +192,8 @@ class DollarTests: XCTestCase {
}

func testFind() {
XCTAssertEqual($.find([1, 2, 3, 4], iterator: { $0 == 2 })!, 2, "Return element when object is found")
XCTAssertNil($.find([1, 2, 3, 4], iterator: { $0 == 10 }) as Int?, "Return nil when object not found")
XCTAssertEqual($.find([1, 2, 3, 4], callback: { $0 == 2 })!, 2, "Return element when object is found")
XCTAssertNil($.find([1, 2, 3, 4], callback: { $0 == 10 }) as Int?, "Return nil when object not found")
}

func testMax() {
Expand Down
49 changes: 44 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,28 @@ $.difference([1, 2, 3, 4, 5], [5, 2, 10])
=> [1, 3, 4]
```

### each - `$.each`

Passes each element in the array to the callback

```swift
$.each(["A", "B"]) {
println("Value \($0)")
}
=> ["A", "B"]

$.each(["A", "B"]) { (index, elem) in
println("\(index) - \(elem)")
}
=> ["A", "B"]
```

### every - `$.every`

Checks if the given callback returns true value for all items in the array.

```swift
$.every([1, 2, 3, 4], iterator: { $0 < 20 })
$.every([1, 2, 3, 4], callback: { $0 < 20 })
=> true

$.every([1, 2, 3, 4]) { $0 == 1 }
Expand Down Expand Up @@ -138,7 +153,7 @@ $.fetch(arr, -1)
Iterates over elements of an array and returning the first element that the callback returns true for.

```swift
$.find([1, 2, 3, 4], iterator: { $0 == 2 })
$.find([1, 2, 3, 4], callback: { $0 == 2 })
=> 2

$.find([1, 2, 3, 4]) { $0 == 10 }
Expand Down Expand Up @@ -966,6 +981,30 @@ let some = array.at(1, 3)
=> ["spam", "eggs"]
```

### `each(callback: (Element) -> ()) -> [Element]`

For each item in the array invoke the callback by passing the elem

```swift
let array = ["foo", "spam", "bar", "eggs"]
array.each {
println($0)
}
=> ["foo", "spam", "bar", "eggs"]
```

### `eachWithIndex(callback: (Int, Element) -> ()) -> [Element]`

For each item in the array invoke the callback by passing the elem along with the index

```swift
let array = ["foo", "spam", "bar", "eggs"]
array.each { (index, elem)
println("\(index) - \(elem)")
}
=> ["foo", "spam", "bar", "eggs"]
```

### `cycle<U>(times: Int, callback: (Element) -> U)`

Cycles through the array definetly or indefinetly passing each element into the callback function. The second parameter is to specify how many times to cycle through the array. If left out it will cycle indefinetly.
Expand All @@ -983,7 +1022,7 @@ Cycles through the array definetly or indefinetly passing each element into the
// Cycles in an infinite loop
```

### `every(iterator: (Element) -> Bool) -> Bool`
### `every(callback: (Element) -> Bool) -> Bool`

Checks if the given callback returns true value for all items in the array.

Expand Down Expand Up @@ -1011,7 +1050,7 @@ arr.fetch(-1)
=> 8
```

### `findIndex(iterator: (Element) -> Bool) -> Int?`
### `findIndex(callback: (Element) -> Bool) -> Int?`

This method is like find except that it returns the index of the first element that passes the callback check.

Expand All @@ -1023,7 +1062,7 @@ ind! == 2
=> true
```

### `findLastIndex(iterator: (Element) -> Bool) -> Int?`
### `findLastIndex(callback: (Element) -> Bool) -> Int?`

This method is like findIndex except that it iterates over elements of the array from right to left.

Expand Down

0 comments on commit 662fb3e

Please sign in to comment.