Skip to content

Commit

Permalink
Merge pull request #102 from pratikmota/unique-feature
Browse files Browse the repository at this point in the history
Adding Block and String support for Unique function
  • Loading branch information
refaktor authored Jan 19, 2024
2 parents 9a040e7 + 9f2eace commit a1cfcc0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
19 changes: 18 additions & 1 deletion evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4246,8 +4246,25 @@ var builtins = map[string]*env.Builtin{
uniqueSlice = append(uniqueSlice, key)
}
return *env.NewList(uniqueSlice)
case env.Block:
uniqueList := util.RemoveDuplicate(ps, block.Series.S)
return *env.NewBlock(*env.NewTSeries(uniqueList))
case env.String:
strSlice := make([]env.Object, 0)
// create string to object slice
for _, value := range block.Value {
// if want to block space then we can add here condition
strSlice = append(strSlice, env.ToRyeValue(value))
}
uniqueStringSlice := util.RemoveDuplicate(ps, strSlice)
uniqueStr := ""
// converting object to string and append final
for _, value := range uniqueStringSlice {
uniqueStr = uniqueStr + env.RyeToRaw(value).(string)
}
return *env.NewString(uniqueStr)
default:
return MakeArgError(ps, 1, []env.Type{env.ListType}, "unique")
return MakeArgError(ps, 1, []env.Type{env.ListType, env.BlockType, env.StringType}, "unique")
}
},
},
Expand Down
18 changes: 17 additions & 1 deletion tests/structures.rye
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,23 @@ section "Working with blocks and lists"
{
equal { { 3 2 3 5 3 2 } .list .unique .length? } 3 ; result order is not deterministic list { 3 2 5 }
}


group "unique"
mold\nowrap ?unique
{ { block string list } }
{
; List
equal { unique list { 1 1 2 2 3 } } list { 1 2 3 }
equal { unique list { 1 1 2 2 } } list { 1 2 }

; Block
equal { unique { 1 1 2 2 3 } } { 1 2 3 }
equal { unique { 1 1 2 2 } } { 1 2 }

; String
equal { unique "aabbc" } "abc"
equal { unique "ab" } "ab"
}
}


Expand Down
26 changes: 26 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,32 @@ func ContainsVal(ps *env.ProgramState, b []env.Object, val env.Object) bool {
return false
}

func RemoveDuplicate(ps *env.ProgramState, slice []env.Object) []env.Object {
allKeys := make(map[env.Object]bool)
list := []env.Object{}
for _, item := range slice {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}

/*
func RemoveDuplicate[T comparable](sliceList []T) []T {
allKeys := make(map[T]bool)
list := []T{}
for _, item := range sliceList {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}
*/

/* func Transpose(slice []env.Object) []env.Object {
yl := len(slice)
var xl int
Expand Down

0 comments on commit a1cfcc0

Please sign in to comment.