Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Commit

Permalink
reduceRight, implode, reduce, explode
Browse files Browse the repository at this point in the history
- Array (reduceRight, implode)
- Dictionary (reduce)
- String (explode)
  • Loading branch information
pNre committed Jun 6, 2014
1 parent f52f69a commit 39d89b9
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 19 deletions.
Binary file not shown.
44 changes: 30 additions & 14 deletions ExSwift/Array.swift
Expand Up @@ -174,21 +174,16 @@ extension Array {
var result = Array<Array<Any?>>()

// Gets the longest array
let max = arrays.reduce(count, combine: {
(max: Int, item: Array<Any>) -> Int in
return item.count > max ? item.count : max;
})
let max = arrays.map { (array: Array<Any>) -> Int in
return array.count
}.max() as Int

for i in 0..max {
var item = Array<Any?>()

item.append(get(i))

for array in arrays {
item.append(array.get(i))
}

result.append(item)
// i-th element in self as array + every i-th element in each array in arrays
result.append([get(i)] + arrays.map {
(array: Array<Any>) -> Any? in return array.get(i)
})

}

Expand Down Expand Up @@ -327,9 +322,9 @@ extension Array {
* Opposite of filter
*/
func reject (exclude: (T -> Bool)) -> Array<T> {
return self.filter({
return filter {
return !exclude($0)
})
}
}

/**
Expand Down Expand Up @@ -388,6 +383,27 @@ extension Array {
return result

}

/**
* Joins the array elements with a separator
* @param separator
* @return Joined object if self is not empty
* and its elements are instances of C, nil otherwise
*/
func implode <C: ExtensibleCollection> (separator: C) -> C? {
if self.first() is C {
return Swift.join(separator, reinterpretCast(self) as Array<C>)
}

return nil
}

/**
* self.reduce from right to left
*/
func reduceRight <U>(initial: U, combine: (U, Element) -> U) -> U {
return self.reverse().reduce(initial, combine: combine)
}

/**
* Removes the last element from self and returns it
Expand Down
12 changes: 10 additions & 2 deletions ExSwift/Dictionary.swift
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Swift

extension Dictionary {

Expand Down Expand Up @@ -76,7 +77,7 @@ extension Dictionary {
* @param testFunction
* @return Filtered dictionary
*/
func filter(testFunction test: (KeyType, ValueType) -> Bool) -> Dictionary<KeyType, ValueType> {
func filter (testFunction test: (KeyType, ValueType) -> Bool) -> Dictionary<KeyType, ValueType> {

var result = Dictionary<KeyType, ValueType>()

Expand Down Expand Up @@ -185,7 +186,14 @@ extension Dictionary {
return false

}


/**
* Same as Array.reduce
*/
func reduce <U> (initial: U, combine: (U, Element) -> U) -> U {
return Swift.reduce(self, initial, combine)
}

/**
* Removes a (key, value) pair from self and returns it as tuple
* @return (key, value)
Expand Down
12 changes: 12 additions & 0 deletions ExSwift/String.swift
Expand Up @@ -51,6 +51,18 @@ extension String {

}

/**
* Returns an array of strings, each of which is a substring of self formed by splitting it on separator
* @param separator
* @return Array of strings
*/
func explode (separator: Character) -> String[] {
return split(self, {
(element: Character) -> Bool in
return element == separator
})
}

/**
* Random string
* @param length String length, 0 -> random length
Expand Down
26 changes: 25 additions & 1 deletion ExSwiftTests/ExSwiftArrayTests.swift
Expand Up @@ -187,12 +187,36 @@ class ExtensionsArrayTests: XCTestCase {
XCTAssert(Array(group[true]!) == [4, 5])
XCTAssert(Array(group[false]!) == [1, 2, 3])
}

func testReduceRight () {
let list = [[0, 1], [2, 3], [4, 5]];
let flat = list.reduceRight(Array<Int>(), { return $0 + $1 });
XCTAssert(flat == [4, 5, 2, 3, 0, 1])
}

func testImplode () {
XCTAssert(["A", "B", "C"].implode("A") == "AABAC")
}

func testPluck () {

let values = [
["Name": "Bob", "Score": 6],
["Name": "Tim", "Score": 8]
]

let result = values.pluck("Score") as Int[]

println(result)

}

/*
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
}
}*/

}
12 changes: 12 additions & 0 deletions ExSwiftTests/ExSwiftDictionaryTests.swift
Expand Up @@ -96,4 +96,16 @@ class ExSwiftDictionaryTests: XCTestCase {
XCTAssertFalse(all)
}

func testReduce () {

let reduced = dictionary.reduce(Dictionary<Int, String>(), {
(var initial: Dictionary<Int, String>, couple: (String, Int)) in
initial.updateValue(couple.0, forKey: couple.1)
return initial
})

XCTAssert(reduced == [2: "B", 3: "C", 1: "A"])

}

}
7 changes: 7 additions & 0 deletions ExSwiftTests/ExSwiftStringTests.swift
Expand Up @@ -33,4 +33,11 @@ class ExSwiftStringTests: XCTestCase {

}

func testExplode () {

let string = "A B C"
XCTAssert(string.explode(" ") == ["A", "B", "C"])

}

}
15 changes: 15 additions & 0 deletions Examples/Array.md
Expand Up @@ -30,6 +30,8 @@
- [`shift`](#shift)
- [`unshift`](#unshift)
- [`groupBy`](#groupby)
- [`reduceRight`](#reduceright)
- [`implode`](#implode)
- [Class Methods](#class-methods)
- [`range`](#range)
- [Operators](#operators)
Expand Down Expand Up @@ -261,6 +263,19 @@ let group = array.groupBy(groupingFunction: {
// → [true: [5, 6], false: [1, 2, 3]]
```

##### `reduceRight` #####
```
let list = [[0, 1], [2, 3], [4, 5]];
list.reduceRight(Array<Int>(), { return $0 + $1 });
// → [4, 5, 2, 3, 0, 1]
```

##### `implode` #####
```
["A", "B", "C"].implode("_")
// → A_B_C
```

### Class Methods ###

##### `range` #####
Expand Down
11 changes: 11 additions & 0 deletions Examples/Dictionary.md
Expand Up @@ -14,6 +14,7 @@
- [`groupBy`](#groupby)
- [`all`](#all)
- [`any`](#any)
- [`reduce`](#reduce)

### Instance Methods ###

Expand Down Expand Up @@ -113,3 +114,13 @@ dictionary.all {
// → false
```

#### `reduce` ####
```swift
let dictionary = [ "A": 1, "B": 2, "C": 3 ]
let reduced = dictionary.reduce(Dictionary<Int, String>(), {
(var initial: Dictionary<Int, String>, couple: (String, Int)) in
initial.updateValue(couple.0, forKey: couple.1)
return initial
})
// → [2: "B", 3: "C", 1: "A"]
```
10 changes: 9 additions & 1 deletion Examples/String.md
Expand Up @@ -4,7 +4,8 @@

- [String](#string)
- [Instance Methods](#instance-methods)
- [`length`](#times)
- [`length`](#length)
- [`explode`](#explode)
- [Class Methods](#class-methods)
- [`random`](#random)
- [Operators](#operators)
Expand All @@ -20,6 +21,13 @@
// → 2
```

##### `explode` #####
```
let string = "A B C"
string.explode(" ")
// → ["A", "B", "C"]
```

### Class Methods ###

##### `random` #####
Expand Down
6 changes: 5 additions & 1 deletion README.md
@@ -1,6 +1,6 @@
# ExSwift

JavaScript inspired (lo-dash, underscore) set of Swift extensions for standard types, functions and classes.
JavaScript (Lo-Dash, Underscore) & Ruby inspired set of Swift extensions for standard types and classes.

## Contents ##

Expand Down Expand Up @@ -57,6 +57,8 @@ Name | Signature
**`shift`**|`shift() -> T`
**`unshift`**|`unshift(newElement: T)`
**`groupBy`**|`groupBy <U> (groupingFunction group: (T) -> (U)) -> Dictionary<U, Array<T>>`
**`reduceRight`**|`reduceRight <U>(initial: U, combine: (U, Element) -> U) -> U`
**`implode`**|`implode <C: ExtensibleCollection> (separator: C) -> C?`

##### Class Methods #####

Expand Down Expand Up @@ -114,6 +116,7 @@ Examples in [Examples/String.md](Examples/String.md)
Name | Signature
---- | ---------
**`length`**|`length () -> Int`
**`explode`**|`explode (separator: Character) -> String[]`

##### Class Methods #####

Expand Down Expand Up @@ -159,6 +162,7 @@ Name | Signatures
**`groupBy`**|`groupBy <T> (groupingFunction group: (KeyType, ValueType) -> (T)) -> Dictionary<T, Array<ValueType>>`
**`any`**|`any (test: (KeyType, ValueType) -> (Bool)) -> Bool`
**`all`**|`all (test: (KeyType, ValueType) -> (Bool)) -> Bool`
**`reduce`**|`reduce <U> (initial: U, combine: (U, Element) -> U) -> U`

### To Do ###
* Compile as library as soon as XCode 6 stops crashing.
Expand Down

0 comments on commit 39d89b9

Please sign in to comment.