Skip to content

Commit

Permalink
added multidimensional versions of several atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
dylannrees authored and DennisMitchell committed May 13, 2018
1 parent 2dfed01 commit 0d8a42c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
63 changes: 63 additions & 0 deletions jelly/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ def dyadic_link(link, args, conv = True, lflat = False, rflat = False):
return [dyadic_link(link, (x, rarg)) for x in larg]
return [dyadic_link(link, (x, y)) for x, y in zip(*args)] + larg[len(rarg) :] + rarg[len(larg) :]

def enumerate_md(array, upper_level = []):
for i, item in enumerate(array):
if type(item) != list:
yield [upper_level + [i + 1], item]
else:
yield from enumerate_md(item, upper_level + [i + 1])

def equal(array):
array = iterable(array)
return int(all(item == array[0] for item in array))
Expand Down Expand Up @@ -321,6 +328,20 @@ def group(array):
except TypeError:
return [grouped[key] for key in sorted(grouped)]

def group_md(array):
array = iterable(array, make_digits = True)
grouped = {}
for index, item in enumerate_md(array):
item = repr(item)
if item in grouped:
grouped[item].append(index)
else:
grouped[item] = [index]
try:
return [grouped[key] for key in sorted(grouped, key = eval)]
except TypeError:
return [grouped[key] for key in sorted(grouped)]

def group_equal(array):
array = iterable(array, make_digits = True)
groups = []
Expand Down Expand Up @@ -350,6 +371,12 @@ def index_of(haystack, needle):
return 1 + index
return 0

def index_of_md(haystack, needle):
for index, item in enumerate_md(haystack):
if item == needle:
return index
return []

def indices_md(array, upper_level = []):
a_indices = []
for i, item in enumerate(array):
Expand Down Expand Up @@ -474,6 +501,18 @@ def maximal_indices(iterable):
maximum = max(iterable or [0])
return [u + 1 for u, v in enumerate(iterable) if v == maximum]

def maximal_indices_md(iterable, upper_level = [], maximum = None):
if maximum == None:
maximum = max(flatten(iterable) or [0])
result = []
for i, item in enumerate(iterable):
if type(item) != list:
if item == maximum:
result.append(upper_level + [i + 1])
else:
result.extend(maximal_indices_md(item, upper_level = upper_level + [i + 1], maximum = maximum))
return result

def median(array):
array = sorted(array)
return div(array[(len(array) - 1) // 2] + array[len(array) // 2], 2)
Expand Down Expand Up @@ -2227,11 +2266,19 @@ def zip_ragged(array):
arity = 1,
call = depth
),
'ŒĖ': attrdict(
arity = 1,
call = enumerate_md
),
'ŒG': attrdict(
arity = 1,
ldepth = 1,
call = get_request
),
'ŒĠ': attrdict(
arity = 1,
call = group_md
),
'Œg': attrdict(
arity = 1,
ldepth = 1,
Expand All @@ -2251,6 +2298,10 @@ def zip_ragged(array):
ldepth = 1,
call = lambda z: to_case(z, lower = True)
),
'ŒM': attrdict(
arity = 1,
call = maximal_indices_md
),
'ŒP': attrdict(
arity = 1,
call = powerset
Expand Down Expand Up @@ -2314,6 +2365,10 @@ def zip_ragged(array):
ldepth = 1,
call = lambda z: python_eval(''.join(map(str, z)))
),
'ŒỤ': attrdict(
arity = 1,
call = lambda z: sorted(indices_md(iterable(z)), key = lambda t: at_index_ndim(t, iterable(z)))
),
'Œu': attrdict(
arity = 1,
ldepth = 1,
Expand Down Expand Up @@ -2440,6 +2495,14 @@ def zip_ragged(array):
rdepth = 0,
call = lambda x, y: jellify(itertools.combinations_with_replacement(iterable(x, make_range = True), y))
),
'œẹ': attrdict(
arity = 2,
call = lambda x, y: [t for t, u in enumerate_md(iterable(x)) if u == y]
),
'œi': attrdict(
arity = 2,
call = index_of_md
),
'œl': attrdict(
arity = 2,
call = lambda x, y: trim(x, iterable(y), left = True)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name = 'jellylanguage',
version = '0.1.13',
version = '0.1.14',
packages = [
'jelly'
],
Expand Down

0 comments on commit 0d8a42c

Please sign in to comment.