Skip to content

Commit

Permalink
Merge 94a5cd9 into 9aa956c
Browse files Browse the repository at this point in the history
  • Loading branch information
odino committed Sep 28, 2020
2 parents 9aa956c + 94a5cd9 commit 8647860
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
2.3.1
2.4.0
9 changes: 9 additions & 0 deletions docs/types/array.md
Expand Up @@ -350,6 +350,15 @@ a.shift() # 1
a # [2, 3]
```
### shuffle()
Shuffles elements in the array:
``` py
a = [1, 2, 3, 4]
a.shuffle() # [3, 1, 2, 4]
```
### some(f)
Returns true when at least one of the elements in the array
Expand Down
10 changes: 10 additions & 0 deletions evaluator/builtin_functions_test.go
Expand Up @@ -385,6 +385,16 @@ func TestReverse(t *testing.T) {
testBuiltinFunction(tests, t)
}

func TestShuffle(t *testing.T) {
tests := []Tests{
{`(1..1000).shuffle().str() != (1..1000).str();`, true},
{`(1..1000).shuffle().len() == (1..1000).len();`, true},
{`(1..1000).shuffle().sort().str() == (1..1000).str();`, true},
}

testBuiltinFunction(tests, t)
}

func TestPush(t *testing.T) {
tests := []Tests{
{`[1, 2].push("a");`, []interface{}{1, 2, "a"}},
Expand Down
36 changes: 32 additions & 4 deletions evaluator/functions.go
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"math"
"math/big"
mrand "math/rand"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -367,6 +368,11 @@ func getFns() map[string]*object.Builtin {
Types: []string{object.ARRAY_OBJ, object.STRING_OBJ},
Fn: reverseFn,
},
// shuffle([1,2,3])
"shuffle": &object.Builtin{
Types: []string{object.ARRAY_OBJ},
Fn: shuffleFn,
},
// push([1,2,3], 4)
"push": &object.Builtin{
Types: []string{object.ARRAY_OBJ},
Expand Down Expand Up @@ -1929,13 +1935,16 @@ func reverseFn(tok token.Token, env *object.Environment, args ...object.Object)

if spec == 0 {
// array
array := args[0].(*object.Array)
elements := args[0].(*object.Array).Elements
length := len(elements)
newElements := make([]object.Object, length, length)
copy(newElements, elements)

for i, j := 0, len(array.Elements)-1; i < j; i, j = i+1, j-1 {
array.Elements[i], array.Elements[j] = array.Elements[j], array.Elements[i]
for i, j := 0, len(newElements)-1; i < j; i, j = i+1, j-1 {
newElements[i], newElements[j] = newElements[j], newElements[i]
}

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

// shuffle([1,2,3])
func shuffleFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
err := validateArgs(tok, "shuffle", args, 1, [][]string{{object.ARRAY_OBJ}})

if err != nil {
return err
}

array := args[0].(*object.Array)
length := len(array.Elements)
newElements := make([]object.Object, length, length)
copy(newElements, array.Elements)

mrand.Seed(time.Now().UnixNano())
mrand.Shuffle(len(newElements), func(i, j int) { newElements[i], newElements[j] = newElements[j], newElements[i] })

return &object.Array{Elements: newElements}
}

// push([1,2,3], 4)
func pushFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
err := validateArgs(tok, "push", args, 2, [][]string{{object.ARRAY_OBJ}, {object.NULL_OBJ,
Expand Down

0 comments on commit 8647860

Please sign in to comment.