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:
BlindCallbackandThrowingBlindCallback- A function that you'd pass to another one as a callback, which doesn't need to know anything nor report anything
CallbackandThrowingCallback- A function that you'd pass to another one as a callback, which needs to know the result of the other one
-
TransformerandThrowingTransformer- A function which can transform one thing into another, like the kind you pass to a
mapfunction
- A function which can transform one thing into another, like the kind you pass to a
-
FilterandThrowingFilter- A function which can filter a sequence of elements, like the kind you pass to a
filterfunction
- A function which can filter a sequence of elements, like the kind you pass to a
GeneratorandThrowingGenerator- 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
-
ReducerandThrowingReducer- A function which can reduce a sequence of elements into one value, like the kind you pass to a
reducefunction
- A function which can reduce a sequence of elements into one value, like the kind you pass to a
-
AllocationReducerandThrowingAllocationReducer- A function which can reduce a sequence of elements into one value by allocating new reductions, like the kind you pass to a
reducefunction. 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
-
CombinatorandThrowingCombinator- 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.