Skip to content
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

Collection initializer with an expression. #537

Merged
merged 8 commits into from
Aug 8, 2018

Conversation

LucianoPAlmeida
Copy link
Member

An convenience init to create a collection of a given size initialized with the values of an expression :))

Checklist

  • I checked the Contributing Guidelines before creating this request.
  • New extensions are written in Swift 4.
  • New extensions support iOS 8.0+ / tvOS 9.0+ / macOS 10.10+ / watchOS 2.0+.
  • I have added tests for new extensions, and they passed.
  • All extensions have a clear comments explaining their functionality, all parameters and return type in English.
  • All extensions are declared as public.
  • I have added a changelog entry describing my changes.

@SwifterSwiftBot
Copy link

SwifterSwiftBot commented Aug 8, 2018

4 Messages
📖 Thank you for submitting a pull request to SwifterSwift. The team will review your submission as soon as possible.
📖 iOS: Executed 502 tests, with 0 failures (0 unexpected) in 2.959 (3.634) seconds
📖 tvOS: Executed 472 tests, with 0 failures (0 unexpected) in 1.997 (2.506) seconds
📖 macOS: Executed 334 tests, with 0 failures (0 unexpected) in 1.298 (1.604) seconds

Generated by 🚫 Danger

@codecov
Copy link

codecov bot commented Aug 8, 2018

Codecov Report

Merging #537 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #537      +/-   ##
=========================================
+ Coverage   93.89%   93.9%   +0.01%     
=========================================
  Files          66      66              
  Lines        2800    2806       +6     
=========================================
+ Hits         2629    2635       +6     
  Misses        171     171
Flag Coverage Δ
#ios 93.9% <100%> (+0.01%) ⬆️
#osx 93.9% <100%> (+4.9%) ⬆️
#tvos 93.9% <100%> (+4.9%) ⬆️
Impacted Files Coverage Δ
...tStdlib/RangeReplaceableCollectionExtensions.swift 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e41d614...02371bd. Read the comment docs.

Copy link
Member

@be-meyer be-meyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really interesting extension.

LGTM 👍

@@ -11,6 +11,13 @@ import XCTest

class RangeReplaceableCollectionTests: XCTestCase {

func testInitExpressionOfSize() {
Copy link
Member

@be-meyer be-meyer Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing the test for condition the size negative or 0 within the test. Could you add this?

marcocapano
marcocapano previously approved these changes Aug 8, 2018
/// - Parameters:
/// - expression: The expression to execute for each position of the collection.
/// - size: The size of the collection.
public init(expression: @autoclosure () -> Element, size: Int) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fascinating extension. My only comment is that I think size is not the right name. size does not mean the number of times you're executing something. count makes more sense here.

I would love it if you added a complementary constructor that I feel is sorely missing from Array (or RangeReplaceableCollection, which is very similar. That is:

init(value: Element, count: Int)

which basically does the same thing as this method except it just calls append(value) instead of executing the expression.

I don't mind adding this myself, it just seems to go hand-in-hand with this one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guykogus Such a constructor already exists on the standard lib called init(repeating:count) and in fact it a use case of that was the reason for me to have to create this one :))
Here's my use case bellow:
screen shot 2018-08-08 at 7 45 19 am

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, how did I miss that?! 🤦‍♂️

@@ -11,6 +11,13 @@ import XCTest

class RangeReplaceableCollectionTests: XCTestCase {

func testInitExpressionOfSize() {
var array = [1, 2, 3]
let newArray = [Int](expression: array.removeLast(), size: array.count)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you do it for array.count + 1? Will it throw an error? Or crash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crash, but I believe it's something to user handle. We can't control what is inside the expression :))

/// - Parameters:
/// - expression: The expression to execute for each position of the collection.
/// - size: The size of the collection.
public init(expression: @autoclosure () -> Element, size: Int) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if it's a throwing closure? Should we mark it as throws and the function as rethrows? I believe that this is pretty standard practice (e.g. removeFirst). That would make the signature:

public init(expression: @autoclosure () throws -> Element, size: Int) rethrows

guykogus
guykogus previously approved these changes Aug 8, 2018
@be-meyer be-meyer merged commit e63ac52 into master Aug 8, 2018
@be-meyer be-meyer deleted the collection-init-expression branch August 8, 2018 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants