Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
glebfann authored Dec 1, 2023
1 parent c5c009d commit 6552d9a
Showing 1 changed file with 100 additions and 2 deletions.
102 changes: 100 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,106 @@ dependencies: [
]
```

## Usage
TBD
## Examples
### [`weakify`](https://github.com/gleb032/FunctionalPrimitives/blob/master/Sources/weakify.swift)
Converts a class method to a closure with a weak reference to the instance of the class.

For example we have `TableViewController` and cell `TableViewCell`:
```swift
class TableViewController: UITableViewController {
...
func handleTextChange(_ text: String) {
// some work
}
}

class TableViewCell: UITableViewCell {
var textChangedHandler: ((String) -> Void)?
...
}
```
We want to inject `handleTextChange` in `TableViewCell` in `cellForRowAt` method:
```swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.textChangedHandler = { self.handleTextChange($0) }
return cell
}
```
This may causes a memory leak. Insted it we can use `weakify`:
```swift
cell.textChangedHandler = weakify(self, in: type(of: self).handleTextChange)
```
Actually this is semantically the same as:
```swift
cell.textChangedHandler = { [weak self] in self?.handleTextChange($0) }
```

### [`papply`](https://github.com/gleb032/FunctionalPrimitives/blob/master/Sources/papply.swift)
Partially apply function.

For example:
```swift
func sum(_ lhs: Int, _ rhs: Int) -> Int {
lhs + rhs
}

let add3 = papply(sum, 3)
let result = add3(4) // result = 7
```

### [`curry`](https://github.com/gleb032/FunctionalPrimitives/blob/master/Sources/curry.swift)/[`uncurry`](https://github.com/gleb032/FunctionalPrimitives/blob/master/Sources/uncurry.swift)
Curries/Uncurries a function with arguments.

For example:
```swift
func sum(_ lhs: Int, _ rhs: Int) -> Int {
lhs + rhs
}

let curriedSum = curry(sum)
print(curriedSum(2)(3)) // 5

let uncurriedSum = uncarry(curriedSum)
print(uncurriedSum(2, 3)) // 5
```

### [`memoized`](https://github.com/gleb032/FunctionalPrimitives/blob/master/Sources/memoized.swift)
Memoization optimisation technique where the return values of a function are cached to avoid repeating the same computation.

For example:
```swift
func fibonacci(_ number: Int) -> Int {
number < 2
? number
: fibonacci(number - 1) + fibonacci(number - 2)
}

print(fibonacci(40) // Very slow... O(e^n) complexity
print(memoized(fibonacci(40)) // O(n) complexity
```

### [`compose`](https://github.com/gleb032/FunctionalPrimitives/blob/master/Sources/compose.swift)
Composes functions, i.e. composition of functions `(A) -> (B)` and `(B) -> (C`) will give `(A) -> (C)`.

For example:
```swift
func double(_ num: Int) -> Int {
return num * 2
}

func add5(_ num: Int) -> Int {
return num + 5
}

func toString(_ num: Int) -> String {
return String(num)
}

let pipeline = compose(double, add5, toString)
let result = pipeline(10) // result = "30"
```


## License

Expand Down

0 comments on commit 6552d9a

Please sign in to comment.