What's New

1.2 - `constant`, another `echo`, and `!`

2020-06-21T21:19:57Z

This release brings three new function generators:

  • echo(_:) - A new echo function which creates and returns a function which only ever returns the value you gave the echo function
  • constant(_:) - Two functions which always return the same value: one just like the above echo function (purely alias for clarity), and one which creates a non-transformer which ignores its input always returns the same value
  • ! - Creates a function which simply inverts the output of the function after the exclamation point

See the Readme for examples!


Patch Changes

  • 1.2.4
    • #13: Made the curry function public
  • 1.2.3
    • Added a dynamic library product whose name is a valid bundle identifier
    • Deprecated previous dynamic library product because its name caused problems when submitting to the App Store
  • 1.2.2
    • Added alternative dynamic library product
  • 1.2.1
    • Added dual licenses

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:

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:

let someFunctions: [BlindCallback] = [
    { print("Hello") },
    { print("World!") },
]

someFunctions.forEach(call)

curry

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]

echo

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.

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 02:38:31 GMT-0900 (Hawaii-Aleutian Daylight Time)