Swiftional
Swiftional introduce some functional primitives that complement the Swift standard library.
Created for writing Swift code with a slight touch of functional programming.
Functions
curry
Converts an uncurried function to a curried function.Example:
(A, B) -> R
becomes
(A) -> (B) -> R
uncurry
Converts a curried function into aan uncurried function.Example:
(A) -> (B) -> R
becomes
(A, B) -> R
partial
Partial application. Applies an argument to a function.Example:
(A, B) -> R
with applied first argument becomes
(B) -> R
identity
Identity combinator function. Returns the input without changing it.constant
The constant combinator function. Ignores the function arguments and always returns the provided value.flip
Flips the arguments of a function.Example:
(A, B) -> R
becomes
(B, A) -> R
with
Calls the specified closure with the given attrubute as its receiver and returns its result.ignored
Ignores the function return and always returns `Void`.weakify
Weakifying function.Example:
// Instead of this:
someObject.onActionClosure = otherObject.someFunc // `otherObject` captured by strong reference
// Use operator:
someObject.onActionClosure = weakify(otherObject) { $0.someFunc() } // `otherObject` is weakified, not captured by strong reference
Extensions
Bool
* `fold` Case analysis for the `Bool` type. Applies the provided closures based on the value.-
foldRunRuns the provided closures based on the content of this value. -
oldEitherCase analysis for theBooltype. Applies the provided closures based on the value and returnEither.
Optional
* `fold` Case analysis for the `Optional` type. Applies the provided closures based on the content of this `Optional` value.Protocols
Applyable
* `apply` Calls the specified closure with Self value as its receiver and returns Self value.appliedCalls the specified closure with Self value as its receiver and returns copy of Self value.
Operators
>>>
Composes a functions and return a function that is the result of applying `g` to the output of `f`.<<<
Composes a functions and return a function that is the result of applying `g` to the output of `f`.|>
Pipe forward. Applies an argument to a function.Example. This:
let result = h(parameter: g(parameter: f(parameter: a)))
Can also be written as:
let result = a |> f |> g |> h
<|
Pipe forward. Applies an argument to a function.Example. This:
let result = h(parameter: g(parameter: f(parameter: a)))
Can also be written as:
let result = h <| g <| f <| a
|>>
Applies a function to an argument an returns callable function.Example. This:
let result = { a in f(parameter: a) }
Can also be written as:
let result = a |>> f
<<|
Applies a function to an argument an returns callable function.Example. This:
let result = { a in f(parameter: a) }
Can also be written as:
let result = f <<| a
~~>
Asynchronous function composition>=>
Effectful function composition?>
Weakifying function.Example:
// Instead of this:
someObject.onActionClosure = otherObject.someFunc // `otherObject` captured by strong reference
// Use operator:
someObject.onActionClosure = otherObject ?> { $0.someFunc() } // `otherObject` is weakified, not captured by strong reference
Types
Either
The type `Either` represents a value of one of these types, but not both: `.left(Left)` or `.right(Right)`.The Either type is shifted to the right by convention.
That is, the .left constructor is usually used to hold errors or secondary data,
while .right is used to store a "correct", primary value - one that can be worked on further.
Wordplay: "Right" also means "Correct".