Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/theokim24601/scope
🌷 Swift-Style Scoping Functions for Readable Code 🤗
https://github.com/theokim24601/scope
cocoapods ios kotlin macos scoping-function swift swift-package-manager tvos watchos xcode
Last synced: about 2 months ago
JSON representation
🌷 Swift-Style Scoping Functions for Readable Code 🤗
- Host: GitHub
- URL: https://github.com/theokim24601/scope
- Owner: theokim24601
- License: mit
- Created: 2018-11-28T16:15:49.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-05-08T14:00:27.000Z (8 months ago)
- Last Synced: 2024-10-28T12:32:23.906Z (about 2 months ago)
- Topics: cocoapods, ios, kotlin, macos, scoping-function, swift, swift-package-manager, tvos, watchos, xcode
- Language: Swift
- Homepage:
- Size: 42 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Scope
Scoping Functions of Swift Style for Readable Code
## 🌷 Usage
**`apply`**, **`also`** , **`let`**, **`with`**, **`run`**
### Reture Value
The scope functions differ by the result they return:
- `apply`, `also` return the context object.
- `let`, `with`, and `run` return the closure result.The return value of `also` and `apply` is the context object itself. so they can be included into call chains as side steps.
```swift
let org = Organization()
.also { print("new podo org") }
.apply {
$0.name = "podo"
$0.member = Member(name: "theo", role: .owner)
}
```They also can be used in return statements of functions returning the context object.
```swift
func newPodoOrg() -> Organization {
Organization().apply {
$0.name = "podo"
$0.member = Member(name: "theo", role: .owner)
}
}
```### apply
Use `apply` for code blocks that don't return a value and mainly operate on the members of the receiver object.
```swift
let org = Organization().apply {
$0.name = "podo"
$0.member = Member(name: "theo", role: .owner)
}
print(org)
```### also
Use `also` for additional actions that don't alter the object, such as logging or printing debug information.
```swift
let member = Member(name: "theo", role: .owner)
let org = Organization()org.also { print("new member") }
.addMember(member)
```### let
`let` can be used to invoke one or more functions on result of call chains.
```swift
let numbers = [1, 5, 10]
numbers.map { $0 * 2 }
.filter { $0 > 3}
.let { $0.count }
````let` is often used for executing a code block only with non-nil values.
```swift
var org: Organization?
org = Organization(name: "podo", member: Member(name: "theo", role: .member))org?.member?.let {
$0.role = .owner // $0 is not nil inside '?.let {}'
}
```### with
A non-extension function: the context object is passed as an argument, and return value is the closure result.
`with` can be read as *"with this object, do the following."*
```swift
let description = with(org) {
"Orgnaization name is \($0.name), "
.appending("member name is \($0.member.name)")
}
print(description)
```### run
Use `run` as a non-extension function.
Non-extension `run` lets you execute a block of several statements where an expression is required.```swift
let hexNumberRegex = run { () -> Regex in
let digits = "0-9"
let hexDigits = "A-Fa-f"
let sign = "+-"return Regex("[\(sign)]?[\(digits)\(hexDigits)]+")
}
```## ⚙️ Installation
- **Using [Swift Package Manager](https://swift.org/package-manager/)**
```swift
import PackageDescriptionlet package = Package(
name: "MyApp",
dependencies: [
.package(url: "https://github.com/hb1love/Scope", .upToNextMajor(from: "2.1.0"))
]
)
```- **Using [CocoaPods](https://cocoapods.org)**
```ruby
pod 'Scope'
```## 👮 License
**Scope** is available under the MIT license. See the [LICENSE](LICENSE) for details.