Skip to content

Commit

Permalink
Add uniquify by function overload to ε
Browse files Browse the repository at this point in the history
Not inspired by a recent code golf challenge totally not.
  • Loading branch information
lyxal committed May 25, 2023
1 parent bc2d5c7 commit 87920ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
31 changes: 15 additions & 16 deletions documents/knowledge/elements.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@
vectorise: true
tests:
- '[["abc", "defg", "h"]] : [3, 4, 1]'
- '[[[1, 2, 3], [4], [5, 6, 7, 8]]] : [3, 1, 4]'
- "[[[1, 2, 3], [4], [5, 6, 7, 8]]] : [3, 1, 4]"

- element: "¨@"
name: Function Call / Declaration
Expand Down Expand Up @@ -2146,14 +2146,15 @@
- '[[1, 22, 333]] : " 3\n 23\n123"'
- '[[]] : ""'
- element: "ε"
name: Absolute Difference / Repeat / Regex match
name: Absolute Difference / Repeat / Regex match / Uniquify by Function
arity: 2
description: Returns the absolute difference / Fills an array of a certain length / Does a regex match
description: Returns the absolute difference / Fills an array of a certain length / Does a regex match / Uniquify by function
overloads:
num-num: abs(a - b)
num-str: "[b] * a"
str-num: "[a] * b"
str-str: regex.match(b, a) (first match of regex b on a)
any-fun: keep only unique elements of a, where f(x) is unique
vectorise: false
tests:
- "[5, 1] : 4"
Expand Down Expand Up @@ -5713,13 +5714,13 @@
description: Diagonals of a matrix, starting with the shortest top diagonal
arity: 1
overloads:
lst: diagonals of a, starting with the shortest top diagonal
lst: diagonals of a, starting with the shortest top diagonal
vectorise: false
tests:
- "[[[1,2,3],[4,5,6],[7,8,9]]] : [[3], [2, 6], [1, 5, 9], [4, 8], [7]]"
- "[[[1,2,3,4],[5,6,7,8],[9,10,11,12]]] : [[4], [3, 8], [2, 7, 12], [1, 6, 11], [5, 10], [9]]"
- "[[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]] : [[3], [2, 6], [1, 5, 9], [4, 8, 12], [7, 11], [10]]"
- "[[[]]] : []"
- "[[[1,2,3],[4,5,6],[7,8,9]]] : [[3], [2, 6], [1, 5, 9], [4, 8], [7]]"
- "[[[1,2,3,4],[5,6,7,8],[9,10,11,12]]] : [[4], [3, 8], [2, 7, 12], [1, 6, 11], [5, 10], [9]]"
- "[[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]] : [[3], [2, 6], [1, 5, 9], [4, 8, 12], [7, 11], [10]]"
- "[[[]]] : []"

- element: "Þḋ"
name: Anti-diagonals
Expand All @@ -5739,14 +5740,14 @@
description: Anti-diagonals of a matrix, starting with the shortest top anti-diagonal
arity: 1
overloads:
lst: anti-diagonals of a, starting with the shortest top anti-diagonal
lst: anti-diagonals of a, starting with the shortest top anti-diagonal
vectorise: false
tests:
- "[[[1,2,3],[4,5,6],[7,8,9]]] : [[1], [2, 4], [3, 5, 7], [6, 8], [9]]"
- "[[[1,2,3,4],[5,6,7,8],[9,10,11,12]]] : [[1], [2, 5], [3, 6, 9], [4, 7, 10], [8, 11], [12]]"
- "[[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]] : [[1], [2, 4], [3, 5, 7], [6, 8, 10], [9, 11], [12]]"
- "[[[1,2,3],[4],[7,8,9]]] : [[1], [2, 4], [3, 7], [8], [9]]"
- "[[[]]] : []"
- "[[[1,2,3],[4,5,6],[7,8,9]]] : [[1], [2, 4], [3, 5, 7], [6, 8], [9]]"
- "[[[1,2,3,4],[5,6,7,8],[9,10,11,12]]] : [[1], [2, 5], [3, 6, 9], [4, 7, 10], [8, 11], [12]]"
- "[[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]] : [[1], [2, 4], [3, 5, 7], [6, 8, 10], [9, 11], [12]]"
- "[[[1,2,3],[4],[7,8,9]]] : [[1], [2, 4], [3, 7], [8], [9]]"
- "[[[]]] : []"

- element: "ÞS"
name: Sublists
Expand Down Expand Up @@ -6373,7 +6374,6 @@
- "[[]] : []"
- "[[8,8,8,8,8,8,6,6,6,6,6,8,8,8,8,8,3,3]] : [[16, 17], [6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 11, 12, 13, 14, 15]]"


- element: "Þ‟"
name: List from Anti-Diagonals
description: List of lists from anti-diagonals of a list
Expand All @@ -6384,7 +6384,6 @@
tests:
- "[[[1], [2, 6], [3, 7, 11], [4, 8, 12, 16], [5, 9, 13, 17, 21], [10, 14, 18, 22], [15, 19, 23], [20, 24], [25]]] : [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]"


- element: "Þ„"
name: List from Diagonals
description: List of lists from diagonals of a list
Expand Down
10 changes: 10 additions & 0 deletions tests/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,16 @@ def test_if_modifier():
assert stack[-1] == [1]


def test_uniquify_by_function():
stack = run_vyxal("1 10 ṡ ⁽∷ ε")
assert stack[-1] == [1, 2]

stack = run_vyxal(
"`abc kfjhdg smdfb n dkfjf mnd weiqwop j wkej qkej madma` ⌈⁽Lε"
)
assert stack[-1] == ["abc", "kfjhdg", "smdfb", "n", "weiqwop", "wkej"]


def test_set_intersect():
stack = run_vyxal("Þ∞:↔")
assert stack[-1][:10] == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Expand Down
19 changes: 19 additions & 0 deletions vyxal/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,27 @@ def absolute_difference(lhs, rhs, ctx):
(num, str) -> Array of length a filled with b
(str, num) -> Array of length b filled with a
(str, str) -> single regex match of b against a
(lst, fun) -> unique list of results of applying fun to each element of lst
"""
ts = vy_type(lhs, rhs)
if types.FunctionType in ts:
# Uniquify by function result
lst, fun = (
(lhs, rhs) if isinstance(rhs, types.FunctionType) else (rhs, lhs)
)
lst = iterable(lst, ctx)

@lazylist_from(lst)
def gen():
seen = set()
for item in lst:
res = safe_apply(fun, item, ctx=ctx)
if res not in seen:
seen.add(res)
yield item

return gen()

return {
(NUMBER_TYPE, NUMBER_TYPE): lambda: abs(lhs - rhs),
(NUMBER_TYPE, str): lambda: [rhs] * lhs,
Expand Down

0 comments on commit 87920ce

Please sign in to comment.