Skip to content

Commit

Permalink
pkg/list: add Reverse function
Browse files Browse the repository at this point in the history
This CL adds a list.Reverse function which reverses a list. It mirrors
the Golang slices.Reverse function.

Also, a typo in the func doc for SortStrings is fixed.

Closes #2505.

Signed-off-by: Noam Dolovich <[email protected]>
Change-Id: I6ef95952a51413c4188919ebbda94804e9635e73
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196212
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
NoamTD authored and mvdan committed Jun 24, 2024
1 parent a94d22f commit f42327c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
15 changes: 15 additions & 0 deletions pkg/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package list

import (
"fmt"
"slices"
"sort"

"cuelang.org/go/cue"
Expand Down Expand Up @@ -215,6 +216,20 @@ func Slice(x []cue.Value, i, j int) ([]cue.Value, error) {
return x[i:j], nil
}

// Reverse reverses a list.
//
// For instance:
//
// Reverse([1, 2, 3, 4])
//
// results in
//
// [4, 3, 2, 1]
func Reverse(x []cue.Value) []cue.Value {
slices.Reverse(x)
return x
}

// MinItems reports whether a has at least n items.
func MinItems(list pkg.List, n int) (bool, error) {
count := len(list.Elems())
Expand Down
12 changes: 12 additions & 0 deletions pkg/list/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/list/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func SortStable(list []cue.Value, cmp cue.Value) (sorted []cue.Value, err error)
return s.ret()
}

// Strings sorts a list of strings in increasing order.
// SortStrings sorts a list of strings in increasing order.
func SortStrings(a []string) []string {
sort.Strings(a)
return a
Expand Down
69 changes: 66 additions & 3 deletions pkg/list/testdata/list.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ maxItems: {
fail1: [0, 1]
}

reverse: {
[string]: {x: _, v: list.Reverse(x)}

t1: x: []
t2: x: [1]
t3: x: [1, 2, 3, 4]

fail1: x: 1
fail2: x: "bad"
}

-- out/list-v3 --
Errors:
repeat.t8.v: error in call to list.Repeat: negative count:
Expand All @@ -74,6 +85,10 @@ minItems.fail1: invalid value [] (does not satisfy list.MinItems(1)): len(list)
maxItems.fail1: invalid value [0,1] (does not satisfy list.MaxItems(1)): len(list) > MaxItems(1) (2 > 1):
./in.cue:53:12
./in.cue:53:26
reverse.fail1.v: cannot use 1 (type int) as list in argument 1 to list.Reverse:
./in.cue:68:15
reverse.fail2.v: cannot use "bad" (type string) as list in argument 1 to list.Reverse:
./in.cue:69:15

Result:
import "list"
Expand Down Expand Up @@ -176,6 +191,28 @@ maxItems: {
ok3: [0, ...]
fail1: _|_ // maxItems.fail1: invalid value [0,1] (does not satisfy list.MaxItems(1)): len(list) > MaxItems(1) (2 > 1)
}
reverse: {
t1: {
x: []
v: []
}
t2: {
x: [1]
v: [1]
}
t3: {
x: [1, 2, 3, 4]
v: [4, 3, 2, 1]
}
fail1: {
x: 1
v: _|_ // reverse.fail1.v: cannot use 1 (type int) as list in argument 1 to list.Reverse
}
fail2: {
x: "bad"
v: _|_ // reverse.fail2.v: cannot use "bad" (type string) as list in argument 1 to list.Reverse
}
}
-- diff/-out/list-v3<==>+out/list --
diff old new
--- old
Expand All @@ -189,9 +226,9 @@ diff old new
./in.cue:53:12
./in.cue:53:26
- ./in.cue:58:9

Result:
import "list"
reverse.fail1.v: cannot use 1 (type int) as list in argument 1 to list.Reverse:
./in.cue:68:15
reverse.fail2.v: cannot use "bad" (type string) as list in argument 1 to list.Reverse:
-- diff/todo/p2 --
Missing error positions.
-- out/list --
Expand All @@ -211,6 +248,10 @@ maxItems.fail1: invalid value [0,1] (does not satisfy list.MaxItems(1)): len(lis
./in.cue:53:12
./in.cue:53:26
./in.cue:58:9
reverse.fail1.v: cannot use 1 (type int) as list in argument 1 to list.Reverse:
./in.cue:68:15
reverse.fail2.v: cannot use "bad" (type string) as list in argument 1 to list.Reverse:
./in.cue:69:15

Result:
import "list"
Expand Down Expand Up @@ -313,3 +354,25 @@ maxItems: {
ok3: [0, ...]
fail1: _|_ // maxItems.fail1: invalid value [0,1] (does not satisfy list.MaxItems(1)): len(list) > MaxItems(1) (2 > 1)
}
reverse: {
t1: {
x: []
v: []
}
t2: {
x: [1]
v: [1]
}
t3: {
x: [1, 2, 3, 4]
v: [4, 3, 2, 1]
}
fail1: {
x: 1
v: _|_ // reverse.fail1.v: cannot use 1 (type int) as list in argument 1 to list.Reverse
}
fail2: {
x: "bad"
v: _|_ // reverse.fail2.v: cannot use "bad" (type string) as list in argument 1 to list.Reverse
}
}

0 comments on commit f42327c

Please sign in to comment.