Skip to content

Commit f42327c

Browse files
NoamTDmvdan
authored andcommitted
pkg/list: add Reverse function
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]>
1 parent a94d22f commit f42327c

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

pkg/list/list.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package list
1717

1818
import (
1919
"fmt"
20+
"slices"
2021
"sort"
2122

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

219+
// Reverse reverses a list.
220+
//
221+
// For instance:
222+
//
223+
// Reverse([1, 2, 3, 4])
224+
//
225+
// results in
226+
//
227+
// [4, 3, 2, 1]
228+
func Reverse(x []cue.Value) []cue.Value {
229+
slices.Reverse(x)
230+
return x
231+
}
232+
218233
// MinItems reports whether a has at least n items.
219234
func MinItems(list pkg.List, n int) (bool, error) {
220235
count := len(list.Elems())

pkg/list/pkg.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/list/sort.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func SortStable(list []cue.Value, cmp cue.Value) (sorted []cue.Value, err error)
218218
return s.ret()
219219
}
220220

221-
// Strings sorts a list of strings in increasing order.
221+
// SortStrings sorts a list of strings in increasing order.
222222
func SortStrings(a []string) []string {
223223
sort.Strings(a)
224224
return a

pkg/list/testdata/list.txtar

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ maxItems: {
5959
fail1: [0, 1]
6060
}
6161

62+
reverse: {
63+
[string]: {x: _, v: list.Reverse(x)}
64+
65+
t1: x: []
66+
t2: x: [1]
67+
t3: x: [1, 2, 3, 4]
68+
69+
fail1: x: 1
70+
fail2: x: "bad"
71+
}
72+
6273
-- out/list-v3 --
6374
Errors:
6475
repeat.t8.v: error in call to list.Repeat: negative count:
@@ -74,6 +85,10 @@ minItems.fail1: invalid value [] (does not satisfy list.MinItems(1)): len(list)
7485
maxItems.fail1: invalid value [0,1] (does not satisfy list.MaxItems(1)): len(list) > MaxItems(1) (2 > 1):
7586
./in.cue:53:12
7687
./in.cue:53:26
88+
reverse.fail1.v: cannot use 1 (type int) as list in argument 1 to list.Reverse:
89+
./in.cue:68:15
90+
reverse.fail2.v: cannot use "bad" (type string) as list in argument 1 to list.Reverse:
91+
./in.cue:69:15
7792

7893
Result:
7994
import "list"
@@ -176,6 +191,28 @@ maxItems: {
176191
ok3: [0, ...]
177192
fail1: _|_ // maxItems.fail1: invalid value [0,1] (does not satisfy list.MaxItems(1)): len(list) > MaxItems(1) (2 > 1)
178193
}
194+
reverse: {
195+
t1: {
196+
x: []
197+
v: []
198+
}
199+
t2: {
200+
x: [1]
201+
v: [1]
202+
}
203+
t3: {
204+
x: [1, 2, 3, 4]
205+
v: [4, 3, 2, 1]
206+
}
207+
fail1: {
208+
x: 1
209+
v: _|_ // reverse.fail1.v: cannot use 1 (type int) as list in argument 1 to list.Reverse
210+
}
211+
fail2: {
212+
x: "bad"
213+
v: _|_ // reverse.fail2.v: cannot use "bad" (type string) as list in argument 1 to list.Reverse
214+
}
215+
}
179216
-- diff/-out/list-v3<==>+out/list --
180217
diff old new
181218
--- old
@@ -189,9 +226,9 @@ diff old new
189226
./in.cue:53:12
190227
./in.cue:53:26
191228
- ./in.cue:58:9
192-
193-
Result:
194-
import "list"
229+
reverse.fail1.v: cannot use 1 (type int) as list in argument 1 to list.Reverse:
230+
./in.cue:68:15
231+
reverse.fail2.v: cannot use "bad" (type string) as list in argument 1 to list.Reverse:
195232
-- diff/todo/p2 --
196233
Missing error positions.
197234
-- out/list --
@@ -211,6 +248,10 @@ maxItems.fail1: invalid value [0,1] (does not satisfy list.MaxItems(1)): len(lis
211248
./in.cue:53:12
212249
./in.cue:53:26
213250
./in.cue:58:9
251+
reverse.fail1.v: cannot use 1 (type int) as list in argument 1 to list.Reverse:
252+
./in.cue:68:15
253+
reverse.fail2.v: cannot use "bad" (type string) as list in argument 1 to list.Reverse:
254+
./in.cue:69:15
214255

215256
Result:
216257
import "list"
@@ -313,3 +354,25 @@ maxItems: {
313354
ok3: [0, ...]
314355
fail1: _|_ // maxItems.fail1: invalid value [0,1] (does not satisfy list.MaxItems(1)): len(list) > MaxItems(1) (2 > 1)
315356
}
357+
reverse: {
358+
t1: {
359+
x: []
360+
v: []
361+
}
362+
t2: {
363+
x: [1]
364+
v: [1]
365+
}
366+
t3: {
367+
x: [1, 2, 3, 4]
368+
v: [4, 3, 2, 1]
369+
}
370+
fail1: {
371+
x: 1
372+
v: _|_ // reverse.fail1.v: cannot use 1 (type int) as list in argument 1 to list.Reverse
373+
}
374+
fail2: {
375+
x: "bad"
376+
v: _|_ // reverse.fail2.v: cannot use "bad" (type string) as list in argument 1 to list.Reverse
377+
}
378+
}

0 commit comments

Comments
 (0)