Some basic tools for functional programming
Simply functions which take a value and do nothing with it. Useful for testing and mocks. For example:
stuff.forEach(blackhole)
Like blackhole
, this simply functions which take a value and do nothing with it. Unlike blackhole
, this will always be optimized away in production code. Useful for giving to functions which demand a callback, when you don't have anything to do after it calls back.
For example:
myObject.someAsyncFunction(onComplete: null)
A utility function which simply calls the given function. This is useful for compressing higher-order functions. For example:
let someFunctions: [BlindCallback] = [
{ print("Hello") },
{ print("World!") },
]
someFunctions.forEach(call)
Converts a non-currying function into a currying function. For example:
let add = curry(+)
[1, 2, 3, 4].map(add(4)) // [5, 6, 7, 8]
Simply returns what it's given. This is useful for reusing the input of higher-order functions. For example:
let withoutNils = arrayOfOptionals.compactMap(echo)
It's also useful for flattening collections of generators. For example:
let values = generators.map(echo)
And for providing a predictable test function. For example:
Lazy(initializer: echo("Foo"))
Simply returns a function with a constant output. This is useful when making a test/preview where you always want the same output.
Lazy(initializer: constant("Foo"))
MyView(textTranslator: constant("Bar")) // imagining `textTranslator` is like `(String) -> String`
Converts any function which returns a Bool
into one which returns the opposite value.
For example:
public extension Array {
func exclude(by filter: @escaping Transformer<Element, Bool>) -> some LazySequenceProtocol {
self.lazy.filter(!mapper)
}
}
Some typealiases for common functions:
BlindCallback
andThrowingBlindCallback
- A function that you'd pass to another one as a callback, which doesn't need to know anything nor report anything
Callback
andThrowingCallback
- A function that you'd pass to another one as a callback, which needs to know the result of the other one
-
Transformer
andThrowingTransformer
- A function which can transform one thing into another, like the kind you pass to a
map
function
- A function which can transform one thing into another, like the kind you pass to a
-
Filter
andThrowingFilter
- A function which can filter a sequence of elements, like the kind you pass to a
filter
function
- A function which can filter a sequence of elements, like the kind you pass to a
Generator
andThrowingGenerator
- A function which can generate one thing without any input, like in an
@autoclosure
- A function which can generate one thing without any input, like in an
-
Reducer
andThrowingReducer
- A function which can reduce a sequence of elements into one value, like the kind you pass to a
reduce
function
- A function which can reduce a sequence of elements into one value, like the kind you pass to a
-
AllocationReducer
andThrowingAllocationReducer
- A function which can reduce a sequence of elements into one value by allocating new reductions, like the kind you pass to a
reduce
function. Often slower thanReducer
.
- A function which can reduce a sequence of elements into one value by allocating new reductions, like the kind you pass to a
-
Combinator
andThrowingCombinator
- A function which can combine two values into one
-
CurriedCombinator
- A function which can combine two values into one, over the course of multiple separate function calls.