Skip to content

Commit 5609cc1

Browse files
committed
Merge branch '2.4.x'
2 parents 9aa956c + 8844749 commit 5609cc1

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.3.1
1+
2.4.0

docs/abs.wasm

7.48 KB
Binary file not shown.

docs/types/array.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,15 @@ a.shift() # 1
350350
a # [2, 3]
351351
```
352352
353+
### shuffle()
354+
355+
Shuffles elements in the array:
356+
357+
``` py
358+
a = [1, 2, 3, 4]
359+
a.shuffle() # [3, 1, 2, 4]
360+
```
361+
353362
### some(f)
354363
355364
Returns true when at least one of the elements in the array

evaluator/builtin_functions_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,16 @@ func TestReverse(t *testing.T) {
385385
testBuiltinFunction(tests, t)
386386
}
387387

388+
func TestShuffle(t *testing.T) {
389+
tests := []Tests{
390+
{`(1..1000).shuffle().str() != (1..1000).str();`, true},
391+
{`(1..1000).shuffle().len() == (1..1000).len();`, true},
392+
{`(1..1000).shuffle().sort().str() == (1..1000).str();`, true},
393+
}
394+
395+
testBuiltinFunction(tests, t)
396+
}
397+
388398
func TestPush(t *testing.T) {
389399
tests := []Tests{
390400
{`[1, 2].push("a");`, []interface{}{1, 2, "a"}},

evaluator/functions.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io/ioutil"
1010
"math"
1111
"math/big"
12+
mrand "math/rand"
1213
"os"
1314
"os/exec"
1415
"os/user"
@@ -367,6 +368,11 @@ func getFns() map[string]*object.Builtin {
367368
Types: []string{object.ARRAY_OBJ, object.STRING_OBJ},
368369
Fn: reverseFn,
369370
},
371+
// shuffle([1,2,3])
372+
"shuffle": &object.Builtin{
373+
Types: []string{object.ARRAY_OBJ},
374+
Fn: shuffleFn,
375+
},
370376
// push([1,2,3], 4)
371377
"push": &object.Builtin{
372378
Types: []string{object.ARRAY_OBJ},
@@ -1929,13 +1935,16 @@ func reverseFn(tok token.Token, env *object.Environment, args ...object.Object)
19291935

19301936
if spec == 0 {
19311937
// array
1932-
array := args[0].(*object.Array)
1938+
elements := args[0].(*object.Array).Elements
1939+
length := len(elements)
1940+
newElements := make([]object.Object, length, length)
1941+
copy(newElements, elements)
19331942

1934-
for i, j := 0, len(array.Elements)-1; i < j; i, j = i+1, j-1 {
1935-
array.Elements[i], array.Elements[j] = array.Elements[j], array.Elements[i]
1943+
for i, j := 0, len(newElements)-1; i < j; i, j = i+1, j-1 {
1944+
newElements[i], newElements[j] = newElements[j], newElements[i]
19361945
}
19371946

1938-
return array
1947+
return &object.Array{Elements: newElements}
19391948
} else {
19401949
// string
19411950
str := []rune(args[0].(*object.String).Value)
@@ -1948,6 +1957,25 @@ func reverseFn(tok token.Token, env *object.Environment, args ...object.Object)
19481957
}
19491958
}
19501959

1960+
// shuffle([1,2,3])
1961+
func shuffleFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
1962+
err := validateArgs(tok, "shuffle", args, 1, [][]string{{object.ARRAY_OBJ}})
1963+
1964+
if err != nil {
1965+
return err
1966+
}
1967+
1968+
array := args[0].(*object.Array)
1969+
length := len(array.Elements)
1970+
newElements := make([]object.Object, length, length)
1971+
copy(newElements, array.Elements)
1972+
1973+
mrand.Seed(time.Now().UnixNano())
1974+
mrand.Shuffle(len(newElements), func(i, j int) { newElements[i], newElements[j] = newElements[j], newElements[i] })
1975+
1976+
return &object.Array{Elements: newElements}
1977+
}
1978+
19511979
// push([1,2,3], 4)
19521980
func pushFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
19531981
err := validateArgs(tok, "push", args, 2, [][]string{{object.ARRAY_OBJ}, {object.NULL_OBJ,

0 commit comments

Comments
 (0)