File tree 3 files changed +37
-40
lines changed
3 files changed +37
-40
lines changed Original file line number Diff line number Diff line change @@ -525,3 +525,40 @@ func must[V any](v V, err error) V {
525
525
}
526
526
return v
527
527
}
528
+
529
+ func takeFirstIP (values ... net.IPNet ) net.IPNet {
530
+ return takeFirstF (values , func (v net.IPNet ) bool {
531
+ return len (v .IP ) != 0 && len (v .Mask ) != 0
532
+ })
533
+ }
534
+
535
+ // takeFirstSlice implements takeFirst for []any.
536
+ // []any is not a comparable type.
537
+ func takeFirstSlice [T any ](values ... []T ) []T {
538
+ return takeFirstF (values , func (v []T ) bool {
539
+ return len (v ) != 0
540
+ })
541
+ }
542
+
543
+ // takeFirstF takes the first value that returns true
544
+ func takeFirstF [Value any ](values []Value , take func (v Value ) bool ) Value {
545
+ for _ , v := range values {
546
+ if take (v ) {
547
+ return v
548
+ }
549
+ }
550
+ // If all empty, return the last element
551
+ if len (values ) > 0 {
552
+ return values [len (values )- 1 ]
553
+ }
554
+ var empty Value
555
+ return empty
556
+ }
557
+
558
+ // takeFirst will take the first non-empty value.
559
+ func takeFirst [Value comparable ](values ... Value ) Value {
560
+ var empty Value
561
+ return takeFirstF (values , func (v Value ) bool {
562
+ return v != empty
563
+ })
564
+ }
File renamed without changes.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments