https://github.com/pisces/functionalswiftkit
This kit help you can write code as functional programming.
https://github.com/pisces/functionalswiftkit
conditional-statements functional-programming grouped optional swift unwrap
Last synced: about 1 month ago
JSON representation
This kit help you can write code as functional programming.
- Host: GitHub
- URL: https://github.com/pisces/functionalswiftkit
- Owner: pisces
- License: mit
- Created: 2019-02-08T07:00:28.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-13T06:29:13.000Z (about 7 years ago)
- Last Synced: 2025-10-01T13:33:41.632Z (6 months ago)
- Topics: conditional-statements, functional-programming, grouped, optional, swift, unwrap
- Language: Swift
- Size: 40 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FunctionalSwiftKit
[](https://travis-ci.org/pisces/FunctionalSwiftKit)
[](https://cocoapods.org/pods/FunctionalSwiftKit)
[](https://cocoapods.org/pods/FunctionalSwiftKit)
[](https://cocoapods.org/pods/FunctionalSwiftKit)
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Using
### Condition
* You can use this instead of condition statement according to functional programming paradigm.
* Ensures thread safety.
* It is simple.
* Global variable.
#### Using single condition statement
```swift
if <> {
} else if <> {
} else {
}
```
```swift
_if(<>) {}
.elseif(<>) {}
.else {}
```
#### Using multiple condition statements with chaining
```swift
if <> {
} else if <> {
} else {
}
if <> {
} else if <> {
} else {
}
```
```swift
_if(<>) {}
.elseif(<>) {}
.else {}
.if(<>) {}
.elseif(<>) {}
.else {}
```
### Array extension
* Here are some frequently used functions with array.
```swift
// Sample struct
struct Model: Hashable {
let uid: String
var hashValue: Int { return uid.hashValue }
static func ==(lhs: Model, rhs: Model) -> Bool {
return lhs.uid == rhs.uid
}
}
```
#### Using function 'grouped' with strings
```swift
let source = ["A", "A", "B", "C"]
let grouped = source.grouped { $0 }
// print -> ["A": ["A", "A"], "B": ["B"], "C": ["C"]]
```
#### Using function 'grouped' with structs
```swift
let source = [Model(uid: "A"), Model(uid: "A"), Model(uid: "B"), Model(uid: "C")]
let grouped = source.grouped { $0.uid }
// print -> ["A": [Model(uid: "A"), Model(uid: "A")], "B": [Model(uid: "B")], "C": [Model(uid: "C")]]
```
#### Using function 'subtracted'
```swift
let source = [Model(uid: "A"), Model(uid: "B")]
let other = [Model(uid: "A")]
let subtracted = source.subtracted(other)
// print -> [Model(uid: "B")]
```
#### Using function 'uniqued'
```swift
let source = [Model(uid: "A"), Model(uid: "A"), Model(uid: "B"), Model(uid: "B")]
let uniqued = source.uniqued()
// print -> [Model(uid: "A"), Model(uid: "B"), Model(uid: "C")]
```
### Optional extension
* You can use unwrap of Optional instead of map when you don't need return element.
```swift
import FunctionalSwiftKit
func testUnwrap() {
let string: String? = "string"
string.unwrap { <>($0) }
}
func testEmpty() {
let string: String? = "string"
string
.map { print($0) }
.empty { print("empty") }
}
```
## Requirements
iOS Deployment Target 9.0 higher
## Installation
FunctionalSwiftKit is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'FunctionalSwiftKit'
```
### Carthage
[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with [Homebrew](http://brew.sh/) using the following command:
```bash
$ brew update
$ brew install carthage
```
To integrate FunctionalSwiftKit into your Xcode project using Carthage, specify it in your `Cartfile`:
```ogdl
github "pisces/FunctionalSwiftKit"
```
Run `carthage update` to build the framework and drag the built `FunctionalSwiftKit.framework` into your Xcode project.
## Author
Steve Kim, hh963103@gmail.com
## License
FunctionalSwiftKit is available under the BSD 2-Clause License license. See the LICENSE file for more info.