Skip to content

funcs.py tests #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 18 commits into
base: overloads
Choose a base branch
from
4 changes: 2 additions & 2 deletions docs/Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
## Installation
Make sure you have Python 3.9 or above.
<!-- ```sh -->
<!-- git clone https://github.com/PyGamer0/flax -->
<!-- git clone https://github.com/zoomlogo/flax -->
<!-- pip install poetry -->
<!-- cd flax -->
<!-- poetry install -->
<!-- poetry run python -m flax <flags, etc> -->
<!-- ``` -->
```sh
git clone https://github.com/PyGamer0/flax
git clone https://github.com/zoomlogo/flax
pip install poetry
cd flax
./install
Expand Down
1 change: 1 addition & 0 deletions docs/elements.csv
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@
"æ»","Mathy Dyadic Diagraphs","Bit shift right."
"ær","Mathy Dyadic Diagraphs","Round."
"æl","Mathy Dyadic Diagraphs","Linear regression."
"æẇ","Mathy Dyadic Diagraphs","Combinations with replacement."
"ŒḂ","Other Monadic Diagraphs","Bounce over each."
"ŒB","Other Monadic Diagraphs","Bounce."
"ŒP","Other Monadic Diagraphs","Is palindrome."
Expand Down
5 changes: 5 additions & 0 deletions docs/elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,11 @@
"type": "Mathy Dyadic Diagraphs",
"description": "Linear regression."
},
{
"element": "æẇ",
"type": "Mathy Dyadic Diagraphs",
"description": "Combinations with replacement."
},
{
"element": "ŒḂ",
"type": "Other Monadic Diagraphs",
Expand Down
1 change: 1 addition & 0 deletions docs/elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ $ Quick Two links as monad.
æ» Mathy Dyadic Diagraphs Bit shift right.
ær Mathy Dyadic Diagraphs Round.
æl Mathy Dyadic Diagraphs Linear regression.
æẇ Mathy Dyadic Diagraphs Combinations with replacement.
ŒḂ Other Monadic Diagraphs Bounce over each.
ŒB Other Monadic Diagraphs Bounce.
ŒP Other Monadic Diagraphs Is palindrome.
Expand Down
4 changes: 4 additions & 0 deletions docs/elements.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,10 @@
type: Mathy Dyadic Diagraphs
description: Linear regression.

- element: æẇ
type: Mathy Dyadic Diagraphs
description: Combinations with replacement.

- element: ŒḂ
type: Other Monadic Diagraphs
description: Bounce over each.
Expand Down
8 changes: 7 additions & 1 deletion flax/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import more_itertools as mit
import operator as ops
import random as Random
import itertools as it
from itertools import zip_longest

from flax.common import *
Expand Down Expand Up @@ -383,10 +384,15 @@
"æl": attrdict(
arity=2, dw=1, dx=1, call=lambda w, x: list(statistics.linear_regression(w, x))
),
"æẇ": attrdict(
arity=2,
dw=0,
call=lambda w, x: list(map(list, it.combinations_with_replacement(x, w))),
),
"ŒB": attrdict(arity=1, call=lambda x: iterable(x) + iterable(x)[::-1]),
"ŒP": attrdict(arity=1, dx=1, call=lambda x: x == x[::-1]),
"ŒĠ": attrdict(arity=1, dx=1, call=get_req),
"ŒE": attrdict(arity=1, call=enumerate_md),
"ŒE": attrdict(arity=1, call=lambda x: list(enumerate_md(x))),
"ŒG": attrdict(arity=1, call=lambda x: group_indicies(x, md=True)),
"ŒM": attrdict(arity=1, call=maximal_indicies_md),
"ŒṪ": attrdict(arity=1, call=lambda x: [i for i, e in enumerate_md(x) if e]),
Expand Down
4 changes: 2 additions & 2 deletions flax/chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def dyadic_link(link, w, x, flat=False):
if call == None:
call = overloads.get("any-any")
if call == None:
raise ValueError
error("call: overload not defined")
return call(x)
elif not flat_w and link.dw > dw:
return dyadic_link(link, [w], x)
Expand Down Expand Up @@ -369,7 +369,7 @@ def monadic_link(link, x, flat=False):
if call == None:
call = overloads.get("any")
if call == None:
raise ValueError
error("call: overload not defined")
return call(x)
elif link.dx > dx:
return monadic_link(link, [x])
Expand Down
9 changes: 5 additions & 4 deletions flax/error.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# error: holds the error handling things
import sys
import flax.common
from colorama import Fore, Style


def error(msg, exit_status=1, prefix="\n'"):
def error(msg, exit_status=1, prefix="'"):
"""error: errors with msg and optional exit_status"""
print(prefix + str(msg), file=sys.stderr)
print(Fore.RED + prefix + str(msg) + Style.RESET_ALL, file=sys.stderr)
exit(exit_status)


def debug(msg, prefix='\n"'):
def debug(msg, prefix='"'):
"""debug: log a debug message to stderr"""
if flax.common.DEBUG:
print(prefix + str(msg), file=sys.stderr)
print(Fore.YELLOW + prefix + str(msg) + Style.RESET_ALL, file=sys.stderr)
34 changes: 17 additions & 17 deletions flax/funcs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""funcs: holds the functions used by atoms"""

import functools
import urllib.request
import itertools
import more_itertools
import copy
Expand Down Expand Up @@ -94,8 +95,8 @@ def base(w, x):
def base_decomp(w, x):
"""base_decomp: base decompression with base w"""
res = ""
x = str(bin(abs(x)))[2:]
return [int(i) for i in split(w, x)]
x = bin(abs(x))[2:]
return [[int(j) for j in i] for i in split(w, x)]


def base_i(w, x):
Expand Down Expand Up @@ -148,7 +149,9 @@ def convolve(w, x):

def depth(x):
"""depth: how deeply x is nested"""
if type2str(x) != "lst":
if type2str(x) == "str":
return 1
elif type2str(x) != "lst":
return 0
else:
if not x:
Expand Down Expand Up @@ -277,11 +280,8 @@ def flatten(x):

def get_req(x):
"""get_req: GET request for url x"""
url = "".join(map(chr, x))
url = (
re.match(r"[A-Za-z][A-Za-z0-9+.-]*://", url) is None and "http://" or ""
) + url
response = urllib_request.request.urlopen(url).read()
url = (re.match(r"[A-Za-z][A-Za-z0-9+.-]*://", x) is None and "http://" or "") + x
response = urllib.request.urlopen(url).read()
try:
return response.decode("utf-8")
except:
Expand All @@ -292,7 +292,7 @@ def grade_down(x):
"""grade_down: grade x in descending order"""
x = iterable(x, digits_=True)
grades = []
for i in reversed(sorted(x)):
for i in more_itertools.unique_everseen(reversed(sorted(x))):
grades.append(find_all(i, x))
return flatten(grades)

Expand All @@ -301,7 +301,7 @@ def grade_up(x):
"""grade_up: grade x in ascending order"""
x = iterable(x, digits_=True)
grades = []
for i in sorted(x):
for i in more_itertools.unique_everseen(sorted(x)):
grades.append(find_all(i, x))
return flatten(grades)

Expand Down Expand Up @@ -333,6 +333,8 @@ def group_indicies(x, md=False):
def index_into(w, x):
"""index_into: index into w with x"""
w = iterable(w, digits_=True)
if len(w) == 0:
return []
x = int(x) if type2strn(x) == "int" else x
if type2strn(x) == "int":
return w[x % len(w)]
Expand All @@ -346,7 +348,7 @@ def index_into_md(w, x):
"""index_into_md: index into w multidimensionally with x"""
res = w
for i in x:
res = index_into(i, res)
res = index_into(res, i)
return res


Expand Down Expand Up @@ -722,12 +724,10 @@ def type2strn(x):

def transpose(x, filler=None):
"""transpose: transpose x"""
return list(
map(
lambda x: list(filter(None.__ne__, x)),
itertools.zip_longest(*map(iterable, x), fillvalue=filler),
)
)
return [
[j for j in i if j is not None]
for i in itertools.zip_longest(*[iterable(i) for i in x], fillvalue=filler)
]


def trim(w, x):
Expand Down
3 changes: 2 additions & 1 deletion flax/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re

from flax.builtins import *
from flax.error import error
from flax.common import TOKEN_TYPE

__all__ = ["TOKEN_TYPE", "tokenise"]
Expand Down Expand Up @@ -74,7 +75,7 @@ def tokenise(program):
elif digraph in quicks:
tokens.append([TOKEN_TYPE.QUICK, digraph])
else:
raise NameError("Digraph not defined.")
error(f'syn: non-existent diagraph "{diagraph}"')
elif head == LIST_DELIMETER_L:
contents = ""
k = 1
Expand Down
2 changes: 1 addition & 1 deletion flax/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def parse(tokens):
if stack == [] and chains == []:
if token[1] in "ⁿ":
break
error(f'not enough links to pop for "{token[1]}"')
error(f'syn: not enough links for "{token[1]}"')
popped.insert(0, (stack or chains).pop())
stack += quicks[token[1]].qlink(popped, trains, index)
chains.append(create_chain(stack, arity, is_forward))
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "flax"
version = "1.1.0"
description = "flax tacit language"
authors = ["PyGamer0"]
authors = ["zoomlogo"]
license = "MIT"

[tool.poetry.scripts]
Expand All @@ -12,6 +12,7 @@ flax = "flax.main:main"
python = ">=3.9"
mpmath = "*"
more_itertools = "*"
colorama = "*"

[tool.poetry.dev-dependencies]
pytest = "*"
Expand Down
Loading
Loading