Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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)
```