CocoaPods trunk is moving to be read-only. Read more on the blog, there are 17 months to go.

FunctionalSwift 1.6.7

FunctionalSwift 1.6.7

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jul 2021
SPMSupports SPM

Maintained by Alexandr Zalutskiy, tverdokhleb, Alexander Zalutskii, sstarzhevskii-mac.



  • By
  • Alexander Zalutskiy

YooMoney Functional Swift library

Functor

Transfer A<T> -> A<U> with T -> U

let ƒ: (T) -> U

let a: T? = T()

let b: U? = ƒ <^> a
let c: U? = U() <^ a
let d: U? = a ^> U()

Applicative

Applicative is a wrapper, that can apply wrapped function to wrapped value.

Applicative transfer A<T> -> A<U> with A<(T) -> U>.

let ƒ: Optional<(T) -> U>

let a: T? = T()

let b: U? = ƒ <*> a

Applicative also has several auxiliary functions for applying a function multiple arguments to the wrapped arguments:

liftA2: ((T, U) -> V, A<T>, A<U>) -> A<V>

liftA3: ((T, U, V) -> W, A<T>, A<U>, A<V>) -> A<W>

Monad

Transfer A<T> -> A<U> with T -> A<U>

let ƒ: (T) -> U?

let a: T? = T()

let b: U? = ƒ -<< a
let c: U? = a >>- ƒ

Monoid

Type which can be composed

struct Sum {
    let number: Integer
    
    init(_ number: Integer) {
        self.number = number
    }
}

extension Sum: Monoid {
    static func mempty() -> Sum {
        return Sum(0)
    }
    
    func mappend(_ monoid: Sum) -> Sum {
        return Sum(number + monoid.number)
    }
}

let numbers = [5, 8, 9, 0, 1, 7, 8]
let sum = numbers.map(Sum.init).mconcat().number // = 38