Skip to content

Commit fa5eca2

Browse files
committed
fn: add PeekMap
This commit adds the `PeekMap` function. This function non-deterministically selects and returns a single key-value pair from the given map.
1 parent c2679ad commit fa5eca2

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

fn/func.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package fn
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
6+
"github.com/lightningnetwork/lnd/fn"
7+
)
48

59
// Reducer represents a function that takes an accumulator and the value, then
610
// returns a new accumulator.
@@ -263,3 +267,19 @@ func Last[T any](xs []*T, pred func(*T) bool) (*T, error) {
263267

264268
return matches[len(matches)-1], nil
265269
}
270+
271+
// KV is a generic struct that holds a key-value pair.
272+
type KV[K any, V any] struct {
273+
Key K
274+
Value V
275+
}
276+
277+
// PeekMap non-deterministically selects and returns a single key-value pair
278+
// from the given map.
279+
func PeekMap[K comparable, V any](m map[K]V) fn.Option[KV[K, V]] {
280+
for k, v := range m {
281+
return fn.Some(KV[K, V]{Key: k, Value: v})
282+
}
283+
284+
return fn.None[KV[K, V]]()
285+
}

0 commit comments

Comments
 (0)