Skip to content

This kit help you can write code as functional programming.

License

Notifications You must be signed in to change notification settings

pisces/FunctionalSwiftKit

Repository files navigation

FunctionalSwiftKit

CI Status Version License Platform

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

if <<true or false>> {
} else if <<true or false>> {
} else {
}
  _if(<<true or false>>) {}
  .elseif(<<true or false>>) {}
  .else {}

Using multiple condition statements with chaining

if <<true or false>> {
} else if <<true or false>> {
} else {
}

if <<true or false>> {
} else if <<true or false>> {
} else {
}
  _if(<<true or false>>) {}
  .elseif(<<true or false>>) {}
  .else {}
  .if(<<true or false>>) {}
  .elseif(<<true or false>>) {}
  .else {}

Array extension

  • Here are some frequently used functions with array.
// 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

let source = ["A", "A", "B", "C"]
let grouped = source.grouped { $0 }
// print -> ["A": ["A", "A"], "B": ["B"], "C": ["C"]]

Using function 'grouped' with structs

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'

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'

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.
import FunctionalSwiftKit

func testUnwrap() {
    let string: String? = "string"
    string.unwrap { <<your function for execution>>($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. To install it, simply add the following line to your Podfile:

pod 'FunctionalSwiftKit'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate FunctionalSwiftKit into your Xcode project using Carthage, specify it in your Cartfile:

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.