Skip to content

Commit

Permalink
#234 Added code to implement for rel.Array.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzhang6222 committed May 12, 2020
1 parent ba370be commit 0e41758
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
21 changes: 4 additions & 17 deletions syntax/std_seq.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,11 @@ var (
typeMethod{reflect.TypeOf(rel.String{}), "contains"}: func(args ...rel.Value) rel.Value {
return rel.NewBool(strings.Contains(mustAsString(args[0]), mustAsString(args[1])))
},
typeMethod{reflect.TypeOf(rel.Array{}), "contains"}: func(args ...rel.Value) rel.Value {
a := args[0].(rel.Array)
switch b := args[1].(type) {
case rel.Array:
return ContainsArray(a, b)
case rel.Value:
arrayEnum, _ := a.ArrayEnumerator()
if arrayEnum != nil {
for arrayEnum.MoveNext() {
if arrayEnum.Current().Equal(b) {
return rel.NewBool(true)
}
}
}

}
return rel.NewBool(false)
typeMethod{reflect.TypeOf(rel.Array{}), "contains"}: func(args ...rel.Value) rel.Value {
return ArrayContains(args[0].(rel.Array), args[1])
},

typeMethod{reflect.TypeOf(rel.Bytes{}), "contains"}: func(args ...rel.Value) rel.Value {
return nil
},
Expand All @@ -132,7 +119,7 @@ var (
)
},
typeMethod{reflect.TypeOf(rel.Array{}), "sub"}: func(args ...rel.Value) rel.Value {
return nil
return ArraySub(args[0].(rel.Array), args[1], args[2])
},
typeMethod{reflect.TypeOf(rel.Bytes{}), "sub"}: func(args ...rel.Value) rel.Value {
return nil
Expand Down
30 changes: 28 additions & 2 deletions syntax/std_seq_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ import (
"github.com/arr-ai/arrai/rel"
)

// ContainsArray check if array a contains array b.
func ContainsArray(a rel.Array, b rel.Array) rel.Value {
// ArrayContains check if array a contains b, and b can be rel.Value or rel.Array.
func ArrayContains(a rel.Array, b rel.Value) rel.Value {
switch b := b.(type) {
case rel.Array:
return arrayContainsArray(a, b)
case rel.Value:
arrayEnum, _ := a.ArrayEnumerator()
if arrayEnum != nil {
for arrayEnum.MoveNext() {
if arrayEnum.Current().Equal(b) {
return rel.NewBool(true)
}
}
}
}
return rel.NewBool(false)
}

func arrayContainsArray(a, b rel.Array) rel.Value {
// Get index of b[0] in a
bOffset := 0
bVals := b.Values()
Expand All @@ -26,3 +43,12 @@ func ContainsArray(a rel.Array, b rel.Array) rel.Value {

return rel.NewBool(false)
}

// ArraySub substitutes all b in a with c.
func ArraySub(a rel.Array, b, c rel.Value) rel.Value {
// switch b := b.(type) {
// case rel.Array:
// return arrayContainsArray(a, b)
// case rel.Value:
return nil
}
11 changes: 10 additions & 1 deletion syntax/std_seq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ func TestContains(t *testing.T) {
AssertCodesEvalToSameValue(t, `false`, `//seq.contains(['A','B','C','D','E'],['B','C','E'])`)
AssertCodesEvalToSameValue(t, `false`, `//seq.contains(['A','B','C','D','E'],['A','B','C','E'])`)
AssertCodesEvalToSameValue(t, `false`, `//seq.contains(['A','B','C','D','E'],['A','B','C','D','E','F'])`)
// bytes

}

///////////////////
func TestStrSub(t *testing.T) {
func TestSub(t *testing.T) {
t.Parallel()
// string
AssertCodesEvalToSameValue(t,
`"this is a test"`,
`//seq.sub("this is not a test", "is not", "is")`)
Expand All @@ -75,6 +78,12 @@ func TestStrSub(t *testing.T) {
`"this is still a test"`,
`//seq.sub("this is still a test", "doesn't matter", "hello there")`)
assertExprPanics(t, `//seq.sub("hello there", "test", 1)`)
// array
// AssertCodesEvalToSameValue(t,
// `"this is a test"`,
// `//seq.sub(["this", "is", "not", "a", "test"], ["is", "not"], "is")`)
// bytes

}

func TestStrSplit(t *testing.T) {
Expand Down

0 comments on commit 0e41758

Please sign in to comment.