Skip to content

Commit b813c5f

Browse files
authored
Merge pull request #3 from RougeWare/feature/echo
Added `echo` functions
2 parents 9750466 + 04bf984 commit b813c5f

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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

5065
Some typealiases for common functions:

Sources/FunctionTools/Echo.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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() }

0 commit comments

Comments
 (0)