mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-26 21:51:27 +02:00
fn: add Filter to List
This commit adds an immutable Filter method to the linked List API. This is useful because there are several instances wherein we iterate through the linked List and only process a subset of it in some way or another.
This commit is contained in:
15
fn/list.go
15
fn/list.go
@@ -300,3 +300,18 @@ func (l *List[A]) PushFrontList(other *List[A]) {
|
||||
n = n.Prev()
|
||||
}
|
||||
}
|
||||
|
||||
// Filter gives a slice of all of the node values that satisfy the given
|
||||
// predicate.
|
||||
func (l *List[A]) Filter(f Pred[A]) []A {
|
||||
var acc []A
|
||||
|
||||
for cursor := l.Front(); cursor != nil; cursor = cursor.Next() {
|
||||
a := cursor.Value
|
||||
if f(a) {
|
||||
acc = append(acc, a)
|
||||
}
|
||||
}
|
||||
|
||||
return acc
|
||||
}
|
||||
|
Reference in New Issue
Block a user