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

[SR-12375] Refactoring: Extract Method to Extension #54811

Open
swift-ci opened this issue Mar 17, 2020 · 1 comment
Open

[SR-12375] Refactoring: Extract Method to Extension #54811

swift-ci opened this issue Mar 17, 2020 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. refactoring Area → source tooling: refactoring

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12375
Radar rdar://problem/60832823
Original Reporter mariusLAN (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Source Tooling
Labels Bug, Refactoring
Assignee None
Priority Medium

md5: a2f9fdaa45b849292dacbd0493741cee

Issue Description:

When writig complex Code FP-Style you may want to give the other Developers a better naming style to see what exactly is going on there without heading in every implementation.

The Input may have an complex reduce, map, compactMap, flatMap, sort, etc.

let foo = [
    "bar": ["baz", "buz"],
    "faz": ["baz", "buz"]
]

foo
    .sorted{ $0.key > $1.key }
    .reduce("") { (result, keyValue) -> String in
        keyValue.key + keyValue.value.joined()
    }

The goal would be to extract the reduce Method to a extension on sorted and name it properly:

let foo = [
    "bar": ["baz", "buz"],
    "faz": ["baz", "buz"]
]

foo
    .sorted{ $0.key > $1.key }
    .glueAllTogether()

extension Sequence where Element == (key: String, value: [String]) {
    func glueAllTogether() -> String {
        reduce("") { (result, keyValue) -> String in
            keyValue.key + keyValue.value.joined()
        }
    }
}

The Code is extracted properly, easy to test and better to read for all devs.

@beccadax
Copy link
Contributor

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. refactoring Area → source tooling: refactoring
Projects
None yet
Development

No branches or pull requests

2 participants