Simply functions which take a value and do nothing with it. Useful for testing and mocks.
For example:
stuff.forEach(blackhole)
null
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)
call
A utility function which simply calls the given function. This is useful for compressing higher-order functions.
For example:
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"))
constant
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)
}
}
Function Types
Some typealiases for common functions:
Callbacks
BlindCallback and ThrowingBlindCallback
A function that you’d pass to another one as a callback, which doesn’t need to know anything nor report anything
Callback and ThrowingCallback
A function that you’d pass to another one as a callback, which needs to know the result of the other one
Transformer family of functions
Transformer and ThrowingTransformer
A function which can transform one thing into another, like the kind you pass to a map function
Filter and ThrowingFilter
A function which can filter a sequence of elements, like the kind you pass to a filter function
Generator family of functions
Generator and ThrowingGenerator
A function which can generate one thing without any input, like in an @autoclosure
Reducer family of functions
Reducer and ThrowingReducer
A function which can reduce a sequence of elements into one value, like the kind you pass to a reduce function
AllocationReducer and ThrowingAllocationReducer
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 than Reducer.
Combinator family
Combinator and ThrowingCombinator
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.
Function Tools
Some basic tools for functional programming
blackhole
Simply functions which take a value and do nothing with it. Useful for testing and mocks. For example:
null
Like
blackhole
, this simply functions which take a value and do nothing with it. Unlikeblackhole
, 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:call
A utility function which simply calls the given function. This is useful for compressing higher-order functions. For example:
curry
Converts a non-currying function into a currying function. For example:
echo
Simply returns what it’s given. This is useful for reusing the input of higher-order functions. For example:
It’s also useful for flattening collections of generators. For example:
And for providing a predictable test function. For example:
constant
Simply returns a function with a constant output. This is useful when making a test/preview where you always want the same output.
!
Converts any function which returns a
Bool
into one which returns the opposite value. For example:Function Types
Some typealiases for common functions:
Callbacks
BlindCallback
andThrowingBlindCallback
Callback
andThrowingCallback
Transformer family of functions
Transformer
andThrowingTransformer
map
functionFilter
andThrowingFilter
filter
functionGenerator family of functions
Generator
andThrowingGenerator
@autoclosure
Reducer family of functions
Reducer
andThrowingReducer
reduce
functionAllocationReducer
andThrowingAllocationReducer
reduce
function. Often slower thanReducer
.Combinator family
Combinator
andThrowingCombinator
CurriedCombinator