Skip to content

Commit

Permalink
array.diff(other_array)
Browse files Browse the repository at this point in the history
Computes the difference between 2 arrays
(elements that are only on either of the 2):

```bash
[1, 2, 3].diff([]) # [1, 2, 3]
[1, 2, 3].diff([3]) # [1, 2]
[1, 2, 3].diff([3, 1]) # [2]
[1, 2, 3].diff([1, 2, 3, 4]) # [4]
```
  • Loading branch information
odino committed Mar 27, 2020
1 parent 04acef6 commit 6f4270b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/types/array.md
Expand Up @@ -376,6 +376,18 @@ Computes the intersection between 2 arrays:
[1, 2, 3].intersect([1, 2, 3, 4]) # [1, 2, 3]
```
### diff(array)
Computes the difference between 2 arrays
(elements that are only on either of the 2):
```bash
[1, 2, 3].diff([]) # [1, 2, 3]
[1, 2, 3].diff([3]) # [1, 2]
[1, 2, 3].diff([3, 1]) # [2]
[1, 2, 3].diff([1, 2, 3, 4]) # [4]
```
## Next
That's about it for this section!
Expand Down
11 changes: 11 additions & 0 deletions evaluator/builtin_functions_test.go
Expand Up @@ -634,6 +634,17 @@ func TestIntersect(t *testing.T) {
testBuiltinFunction(tests, t)
}

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

testBuiltinFunction(tests, t)
}

func testBuiltinFunction(tests []Tests, t *testing.T) {
for _, tt := range tests {
evaluated := testEval(tt.input)
Expand Down
45 changes: 45 additions & 0 deletions evaluator/functions.go
Expand Up @@ -203,6 +203,11 @@ func getFns() map[string]*object.Builtin {
Types: []string{object.ARRAY_OBJ},
Fn: intersectFn,
},
// diff(array:[1, 2, 3], array:[1, 2, 3])
"diff": &object.Builtin{
Types: []string{object.ARRAY_OBJ},
Fn: diffFn,
},
// map(array:[1, 2, 3], function:f(x) { x + 1 })
"map": &object.Builtin{
Types: []string{object.ARRAY_OBJ},
Expand Down Expand Up @@ -1204,6 +1209,46 @@ func intersectFn(tok token.Token, env *object.Environment, args ...object.Object
return &object.Array{Elements: intersection}
}

// intersect(array:[1, 2, 3], array:[1, 2, 3])
func diffFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
err := validateArgs(tok, "diff", args, 2, [][]string{{object.ARRAY_OBJ}, {object.ARRAY_OBJ}})
if err != nil {
return err
}

left := args[0].(*object.Array).Elements
right := args[1].(*object.Array).Elements
foundLeft := map[string]object.Object{}
foundRight := map[string]object.Object{}
difference := []object.Object{}

for _, o := range left {
foundLeft[string(o.Type())+"__"+o.Inspect()] = o
}

for _, o := range right {
foundRight[string(o.Type())+"__"+o.Inspect()] = o
}

for _, o := range left {
_, ok := foundRight[string(o.Type())+"__"+o.Inspect()]

if !ok {
difference = append(difference, o)
}
}

for _, o := range right {
_, ok := foundLeft[string(o.Type())+"__"+o.Inspect()]

if !ok {
difference = append(difference, o)
}
}

return &object.Array{Elements: difference}
}

// map(array:[1, 2, 3], function:f(x) { x + 1 })
func mapFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
err := validateArgs(tok, "map", args, 2, [][]string{{object.ARRAY_OBJ}, {object.FUNCTION_OBJ, object.BUILTIN_OBJ}})
Expand Down

0 comments on commit 6f4270b

Please sign in to comment.