-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
[stdlib] Used map and guard for nil handling #241
Conversation
@@ -177,9 +177,8 @@ public struct EnumerateGenerator< | |||
/// | |||
/// - Requires: No preceding call to `self.next()` has returned `nil`. | |||
public mutating func next() -> Element? { | |||
let b = base.next() |
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.
This could be implemented using Optional.map
:
public mutating func next() -> Element? {
return base.next().map { (index: count++, element: $0) }
}
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.
-1 from me on this one because of side effects inside map()
.
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.
@gribozavr Changed it to guard let. What side effect it cause?
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 increment in index++
was side effect.
@NachoSoto Thinking about that, also find these changes in #193. I am making these changes. Thanks for suggestion. |
Done changes to handle nil by |
@gribozavr Thanks for reviewing this. I have done the changes. I have also done some performance testing and I get same results. All |
@codestergit I ran the tests and couldn't reproduce the failure. |
[stdlib] Used map and guard for nil handling
@gribozavr Thank for merging. Sorry but just for my knowledge want to know the index++ side effect. I am not that much good with higher order functions.How map will affect or behave different way. public func map<U>(@noescape f: (Wrapped) throws -> U) rethrows -> U? {
switch self {
case .Some(let y):
return .Some(try f(y))
case .None:
return .None //I think it will return as it is and f will not execute and `index++` should not happen
}
} so it is also returning early if our |
@codestergit It should behave correctly just as you expect, it is just poor style to use side-effecting operations with APIs influenced by the functional paradigm, like |
Thanks @gribozavr . |
Rework OrderedImports rule.
…hanges Update gyb generated files for _specialize syntax addition
Separate vapor project into subcomponents (SR-7233)
Changed nil handling by using map statements.
I think these changes should have
guard let
ormap
as it is more expressive.Please let me know if anything else needed.
Thanks to swift team