File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed
Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,21 @@ let add = curry(+)
4545```
4646
4747
48+ ## ` echo ` ##
49+
50+ Simply returns what it's given. This is useful for reusing the input of higher-order functions.
51+ For example:
52+ ``` swift
53+ let withoutNils = arrayOfOptionals.compactMap (echo)
54+ ```
55+
56+ It's also useful for flattening collections of generators.
57+ For example:
58+ ``` swift
59+ let values = generators.map (echo)
60+ ```
61+
62+
4863## Function Types ##
4964
5065Some typealiases for common functions:
Original file line number Diff line number Diff line change 1+ //
2+ // Echo.swift
3+ // FunctionTools
4+ //
5+ // Created by Ben Leggiero on 2020-05-20.
6+ // Copyright © 2020 Ben Leggiero BS-1-PS.
7+ //
8+
9+ import Foundation
10+
11+
12+
13+ /// A utility function which simply returns the given value.
14+ /// This is useful for reusing the input of higher-order functions.
15+ ///
16+ /// For example:
17+ /// ```swift
18+ /// let withoutNils = arrayOfOptionals.compactMap(echo)
19+ /// ```
20+ ///
21+ /// - Parameter input: The value to return
22+ @inlinable
23+ public func echo< T> ( _ input: T ) -> T { input }
24+
25+
26+ /// A utility function which simply returns the result of the given function.
27+ /// This is useful for flattening collections of generators.
28+ ///
29+ /// For example:
30+ /// ```swift
31+ /// let values = generators.map(echo)
32+ /// ```
33+ ///
34+ /// - Parameter inputGenerator: The function which generates a value
35+ /// - Throws: Anything `inputGenerator` throws
36+ @inlinable
37+ public func echo< T> ( _ inputGenerator: ThrowingGenerator < T > ) rethrows -> T { try inputGenerator ( ) }
You can’t perform that action at this time.
0 commit comments