-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'removeRandomElement()' to Dictionary #497
Add 'removeRandomElement()' to Dictionary #497
Conversation
SwiftLint found issuesWarnings
Generated by 🚫 Danger |
@@ -34,6 +34,14 @@ public extension Dictionary { | |||
keys.forEach { removeValue(forKey: $0) } | |||
} | |||
|
|||
/// SwifterSwift: Remove a random element from the dictionary. | |||
@discardableResult public mutating func removeRandomElement() -> Value? { | |||
if let key = Array(keys).randomItem { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately you can't use SwifterSwift
functions internally. The project is designed so that each function can be copy-pasted as-is into your app. The main reason is that the whole library itself can't safely be added as a dependency because of potential name clashes with other 3rd party libraries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I will adjust the code accordingly.
Codecov Report
@@ Coverage Diff @@
## master #497 +/- ##
==========================================
+ Coverage 93.82% 93.84% +0.01%
==========================================
Files 66 66
Lines 2771 2778 +7
==========================================
+ Hits 2600 2607 +7
Misses 171 171
Continue to review full report at Codecov.
|
71f7fd2
to
bb47c05
Compare
@Vyax Thanks for your pull request! This definitely feels like an odd extension though. What's your use case? |
@SD10 I could imagin if you have something like a card game and you want to draw cards of a stack/array. But I agree, seems really odd/special. |
bb47c05
to
9ad9e48
Compare
I’ve used a similar extension a few times in game development - I think it’s got a solid use case :) |
@oliviabrown9 @Bennx Thanks for the feedback and use cases! I wonder if this extension could be made on |
@SD10 this can't be made generic because extension RangeReplaceableCollection {
@discardableResult public mutating func removeRandomElement() -> Element {
return remove(at: index(startIndex,
offsetBy: numericCast(arc4random_uniform(numericCast(count)))))
}
}
var arr = [0, 1, 2, 3, 4]
arr.removeRandomElement() // 2
arr // [0, 1, 3, 4] |
Hy guys |
@LucianoPAlmeida I think |
@SD10 That makes sense 👍 |
The method of |
9ad9e48
to
b0d6053
Compare
CHANGELOG.md
Outdated
- **Optional**: | ||
- Added `isNilOrEmpty` property to check whether an optional is nil or empty collection. | ||
- **UIWindow**: | ||
- Added `switchRootViewController` method to switch root view controller with animation. [#494](https://github.com/SwifterSwift/SwifterSwift/pull/494) by [omaralbeik](https://github.com/omaralbeik). | ||
|
||
- **Optional**: | ||
- Added `isNilOrEmpty` property to check whether an optional is nil or empty collection. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've duplicated this line
7a586f7
to
c8672b6
Compare
I think this pull request should be ready for merging now. |
CHANGELOG.md
Outdated
- **UIEdgeInsets** | ||
- Added `horizontal` and `vertical` properties. Also `init(inset:)` and `init(horizontal: vertical:)` initializers for convenience. [#500](https://github.com/SwifterSwift/SwifterSwift/pull/500) by [LucianoPAlmeida](https://github.com/LucianoPAlmeida). | ||
- **UIGestureRecognizer**: | ||
- Added `removeFromView()` method to remove recognizer from the view the recognizer is attached to. [#456](https://github.com/SwifterSwift/SwifterSwift/pull/456) by [mmdock](https://github.com/mmdock). | ||
- **UIScrollView**: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's up with this diff on the CHANGELOG?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sorted the changes alphabetically.
e4a9b01
to
f9aa957
Compare
@@ -26,6 +26,17 @@ final class DictionaryExtensionsTests: XCTestCase { | |||
XCTAssertFalse(dict.keys.contains("key2")) | |||
} | |||
|
|||
func testRemoveElementForRandomKey() { | |||
var emptyDict = [String: String]() | |||
emptyDict.removeValueForRandomKey() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to use XCTAssertNil
, like you did for RangeReplaceableCollectionExtTests
?
import XCTest | ||
@testable import SwifterSwift | ||
|
||
final class RangeReplaceableCollectionExtTests: XCTestCase { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class name should match the file name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but swiftlint doesn't allow class names longer than 40 characters causing the Travis build to fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can hint to swiftlint to ignore a specific rule for a specific line by adding a comment above it like this:
// swiftlint:disable next identifier_name
I'm not sure if identifier_name
is the exact rule that's being broken by the long class name, but it's something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we already have this file and class created on master, it was created in #512 if I'm remembering correctly :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah true, and the class name ended up being RangeReplaceableCollectionTests
, I'm guessing to avoid that issue.
I forgot why this didn't get merged but now there's a few review conflicts + comments |
…t()' to RangeReplaceableCollection
f9aa957
to
7ab12a7
Compare
Whats the status on this one? I see it's been updated |
…to remove a random element from a dictionary
Checklist