From e822f96da26ff7c5b2f1bdf904f5da6ed0e11a1f Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Thu, 18 Jan 2024 07:26:16 +0530 Subject: [PATCH 1/3] Adding common util function --- util/util.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/util/util.go b/util/util.go index b8dd0b9f..e0d4bc49 100644 --- a/util/util.go +++ b/util/util.go @@ -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 From 6daee501af3ac86f7a73b5d3f78521b163b47b63 Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Thu, 18 Jan 2024 07:28:04 +0530 Subject: [PATCH 2/3] unique block and string logic --- evaldo/builtins.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/evaldo/builtins.go b/evaldo/builtins.go index 515ecd02..090ef0b6 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -4221,8 +4221,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") } }, }, From 4c714912857481ffe0e8133b37d3d6fff2959d9b Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Thu, 18 Jan 2024 07:48:21 +0530 Subject: [PATCH 3/3] test cases added for unique --- tests/structures.rye | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/structures.rye b/tests/structures.rye index 915f023f..df362576 100644 --- a/tests/structures.rye +++ b/tests/structures.rye @@ -237,7 +237,22 @@ 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" + } }