-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
7 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,6 @@ const ( | |
TRUNCATE = "truncate" | ||
SPLIT = "split" | ||
RANGE = "range" | ||
UNIQUE = "unique" | ||
CONFIRM = "confirm" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,5 @@ markdown_extensions: | |
- admonition | ||
- pymdownx.details | ||
- pymdownx.superfences | ||
- toc: | ||
permalink: true # |