Skip to content

Commit

Permalink
Merge cc7940d into 417b4e8
Browse files Browse the repository at this point in the history
  • Loading branch information
odino committed Mar 28, 2020
2 parents 417b4e8 + cc7940d commit 25cad20
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/types/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,15 @@ Flattens an array a single level deep:
[[1, 2, 3, 4]].flatten() # [1, 2, 3, 4]
```
### flatten_deep()
Flattens an array recursively until no member is an array:
```py
[[[1, 2], [[[[3]]]], [4]]].flatten_deep() # [1, 2, 3, 4]
[[1, [2, 3], 4]].flatten_deep() # [1, 2, 3, 4]
```
## Next
That's about it for this section!
Expand Down
13 changes: 13 additions & 0 deletions evaluator/builtin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,19 @@ func TestFlatten(t *testing.T) {
testBuiltinFunction(tests, t)
}

func TestFlattenDeep(t *testing.T) {
tests := []Tests{
{`[1, 2, 3].flatten_deep()`, []int{1, 2, 3}},
{`[1, 2, [3]].flatten_deep()`, []int{1, 2, 3}},
{`[1, 2, [3, 4]].flatten_deep()`, []int{1, 2, 3, 4}},
{`[[1, 2], [3, 4]].flatten_deep()`, []int{1, 2, 3, 4}},
{`[[1, [2]], [3, 4]].flatten_deep()`, []int{1, 2, 3, 4}},
{`[[[1, [2]], [3, 4]]].flatten_deep()`, []int{1, 2, 3, 4}},
}

testBuiltinFunction(tests, t)
}

func testBuiltinFunction(tests []Tests, t *testing.T) {
for _, tt := range tests {
evaluated := testEval(tt.input)
Expand Down
22 changes: 20 additions & 2 deletions evaluator/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ func getFns() map[string]*object.Builtin {
Types: []string{object.ARRAY_OBJ},
Fn: flattenFn,
},
// flattem(array:[1, 2, 3])
"flatten_deep": &object.Builtin{
Types: []string{object.ARRAY_OBJ},
Fn: flattenDeepFn,
},
// map(array:[1, 2, 3], function:f(x) { x + 1 })
"map": &object.Builtin{
Types: []string{object.ARRAY_OBJ},
Expand Down Expand Up @@ -1297,6 +1302,15 @@ func unionFn(tok token.Token, env *object.Environment, args ...object.Object) ob

// flatten(array:[1, 2, 3])
func flattenFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
return flatten("flatten", false, tok, env, args...)
}

// flatten_deep(array:[1, 2, 3])
func flattenDeepFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
return flatten("flatten_deep", true, tok, env, args...)
}

func flatten(fnName string, deep bool, tok token.Token, env *object.Environment, args ...object.Object) object.Object {
err := validateArgs(tok, "flatten", args, 1, [][]string{{object.ARRAY_OBJ}})
if err != nil {
return err
Expand All @@ -1308,8 +1322,12 @@ func flattenFn(tok token.Token, env *object.Environment, args ...object.Object)
for _, v := range originalElements {
switch e := v.(type) {
case *object.Array:
for _, x := range e.Elements {
elements = append(elements, x)
if deep {
elements = append(elements, flattenDeepFn(tok, env, e).(*object.Array).Elements...)
} else {
for _, x := range e.Elements {
elements = append(elements, x)
}
}
default:
elements = append(elements, e)
Expand Down

0 comments on commit 25cad20

Please sign in to comment.