Skip to content

Commit

Permalink
Implement 'unique' function
Browse files Browse the repository at this point in the history
Simply takes in a list and returns one back with only unique items, in
the order of first occurrence.

This commit also adds anchor links to headers in the MkDocs website.
  • Loading branch information
amterp committed Nov 10, 2024
1 parent a21ad1f commit de1a15f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ const (
TRUNCATE = "truncate"
SPLIT = "split"
RANGE = "range"
UNIQUE = "unique"
CONFIRM = "confirm"
)
21 changes: 21 additions & 0 deletions core/rsl_func_unique.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core

import (
"fmt"
"github.com/samber/lo"
)

// todo allow string input? to avoid needing to split(input, "") first
func runUnique(i *MainInterpreter, function Token, args []interface{}) interface{} {
if len(args) != 1 {
i.error(function, UNIQUE+fmt.Sprintf("() takes 1 argument, got %d", len(args)))
}

switch arr := args[0].(type) {
case []interface{}:
return lo.Uniq(arr)
default:
i.error(function, UNIQUE+fmt.Sprintf("() takes an array as its argument, got %s", TypeAsString(args[0])))
panic(UNREACHABLE)
}
}
4 changes: 4 additions & 0 deletions core/rsl_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func RunRslNonVoidFunction(
assertExpectedNumReturnValues(i, function, functionName, numExpectedReturnValues, 1)
validateExpectedNamedArgs(i, function, NO_NAMED_ARGS, namedArgsMap)
return runRange(i, function, args)
case UNIQUE:
assertExpectedNumReturnValues(i, function, functionName, numExpectedReturnValues, 1)
validateExpectedNamedArgs(i, function, NO_NAMED_ARGS, namedArgsMap)
return runUnique(i, function, args)
case CONFIRM:
assertExpectedNumReturnValues(i, function, functionName, numExpectedReturnValues, 1)
validateExpectedNamedArgs(i, function, NO_NAMED_ARGS, namedArgsMap)
Expand Down
10 changes: 10 additions & 0 deletions core/testing/split_test.go → core/testing/func_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ print(split("Alice Smith", " +"))
assertNoErrors(t)
resetTestState()
}

func TestSplit_CanSplitOnNoSeparater(t *testing.T) {
rsl := `
print(split("Alice", ""))
`
setupAndRunCode(t, rsl)
assertOnlyOutput(t, stdOutBuffer, "[A, l, i, c, e]\n")
assertNoErrors(t)
resetTestState()
}
35 changes: 35 additions & 0 deletions core/testing/func_unique_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package testing

import "testing"

func TestUnique(t *testing.T) {
rsl := `
print(unique([2, 1, 2, 3, 1, "Alice", 4, 3, 5, 5]))
`
setupAndRunCode(t, rsl)
assertOnlyOutput(t, stdOutBuffer, "[2, 1, 3, Alice, 4, 5]\n")
assertNoErrors(t)
resetTestState()
}

func TestUnique_Large(t *testing.T) {
rsl := `
a = unique([2 for i in range(1000)])
print(len(a))
print(a[0])
`
setupAndRunCode(t, rsl)
assertOnlyOutput(t, stdOutBuffer, "1\n2\n")
assertNoErrors(t)
resetTestState()
}

func TestUnique_String(t *testing.T) {
rsl := `
print(join(unique(split("Frodo Baggins is a hobbit", "")), ""))
`
setupAndRunCode(t, rsl)
assertOnlyOutput(t, stdOutBuffer, "Frod Baginshbt\n")
assertNoErrors(t)
resetTestState()
}
22 changes: 16 additions & 6 deletions docs-web/docs/reference/ref-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ Are you sure? > n
Unsure!
```

### join

```rsl
join(input any[], prefix string|int|float|bool?, suffix string|int|float|bool?) -> string
```

### unique

```rsl
unique(input any[]) -> any[]
```

```rsl
unique([2, 1, 2, 3, 1, 3, 4]) // [2, 1, 3, 4]
```

## Time

### now_date
Expand Down Expand Up @@ -243,12 +259,6 @@ replace(input, "Charlie (.*)", "Alice $1")
"Alice Brown"
```

### join

```rsl
join(input any[], prefix string|int|float|bool?, suffix string|int|float|bool?) -> string
```

### starts_with

```rsl
Expand Down
2 changes: 2 additions & 0 deletions docs-web/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ markdown_extensions:
- admonition
- pymdownx.details
- pymdownx.superfences
- toc:
permalink: true #

0 comments on commit de1a15f

Please sign in to comment.