Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/0xleif/closure
Define and chain Closures with Inputs and Outputs
https://github.com/0xleif/closure
chain closure closures functional-programming functions scope state swift then thenable
Last synced: 14 days ago
JSON representation
Define and chain Closures with Inputs and Outputs
- Host: GitHub
- URL: https://github.com/0xleif/closure
- Owner: 0xLeif
- Created: 2021-10-05T00:35:53.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-05T00:36:53.000Z (about 3 years ago)
- Last Synced: 2024-10-30T17:15:49.085Z (14 days ago)
- Topics: chain, closure, closures, functional-programming, functions, scope, state, swift, then, thenable
- Language: Swift
- Homepage:
- Size: 1.95 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Closure
*Define and chain Closures with Inputs and Outputs*
## Examples
**No Scoped State**
```swift
let noStateCount = Closure { text in
String(repeating: text, count: 4)
}
.then { string in
Int(string) ?? 0
}XCTAssertEqual(noStateCount.method("5"), 5555)
XCTAssertEqual(noStateCount.method("5"), 5555)
XCTAssertEqual(noStateCount.method("5"), 5555)
```**Scoped State**
```swift
let stateCount: Closure = Closure {
var count = 1
return { text in
defer {
count += 1
}
return String(repeating: text, count: count)
}
}
.then { string in
Int(string) ?? 0
}XCTAssertEqual(stateCount.method("5"), 5)
XCTAssertEqual(stateCount.method("5"), 55)
XCTAssertEqual(stateCount.method("5"), 555)
```