mirror of
https://github.com/samsonjs/SwiftBatteries.git
synced 2026-04-27 15:07:46 +00:00
simplify
This commit is contained in:
parent
543ac54165
commit
7012b60ebb
1 changed files with 6 additions and 6 deletions
|
|
@ -26,13 +26,13 @@ public extension CollectionType {
|
|||
|
||||
public extension CollectionType where Index: RandomAccessIndexType {
|
||||
|
||||
/// Returns a random element, or nil if the collection is empty
|
||||
/// Returns a random element, or nil if the collection is empty.
|
||||
func sample() -> Generator.Element? {
|
||||
guard !isEmpty else {
|
||||
return nil
|
||||
}
|
||||
let n = Int(count.toIntMax())
|
||||
let i = startIndex.advancedBy(Index.Distance(IntMax(randomInt(n) - 1)))
|
||||
let n = randomInt(Int(count.toIntMax()))
|
||||
let i = startIndex.advancedBy(Index.Distance(IntMax(n)))
|
||||
return self[i]
|
||||
}
|
||||
|
||||
|
|
@ -44,11 +44,11 @@ public extension CollectionType where Generator.Element: Equatable {
|
|||
/// identical elements.
|
||||
func unique() -> [Generator.Element] {
|
||||
var last: Generator.Element?
|
||||
return filter({ item -> Bool in
|
||||
let isUnique: Bool = last == nil || last != item
|
||||
return filter { item in
|
||||
let isUnique = last == nil || last != item
|
||||
last = item
|
||||
return isUnique
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue