Skip to content
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

Custom functions #38

Open
RustemB opened this issue Aug 30, 2019 · 3 comments
Open

Custom functions #38

RustemB opened this issue Aug 30, 2019 · 3 comments

Comments

@RustemB
Copy link

RustemB commented Aug 30, 2019

How about custom functions support (or plugins)
Like:

seq 5 | filter is_odd

will return:

1
3
5

where is_odd is custom function (e.g. in ~/.config/shell-functools/plugins/main.py directory)

@functools.func("Int", "Bool") #or @functools.func(int, bool)
def is_odd(a: int) -> bool:
    return a % 2 == 1

but, yes, it looks very complicated...

@sharkdp
Copy link
Owner

sharkdp commented Aug 31, 2019

Interesting idea, thank you!

@jdhedden
Copy link

jdhedden commented May 9, 2022

Offering

For README.md:

Usage of condition

The condition function permits filtering using a definition string to
Python's lambda function.

> seq -3 3 | filter condition 'x : x>0'
1
2
3

> seq -5 5 | filter condition 'x : x%3 == 0 or x == 1'
-3
0
1
3

> echo -e '1\t2\n3\t2\n1\t1' | filter cond 'x,y: x>y'
3	2

For ft/ft/functions.py:

@register("cond", "condition")
@typed(None, T_BOOL)
def condition(definition, inp):
    _lambda = 'lambda ' + definition.value
    if type(inp) == list:
        args = list(map(lambda v: v.value, inp))
        return (eval(_lambda))(*args)
    else:
        return (eval(_lambda))(inp)

@sharkdp
Copy link
Owner

sharkdp commented May 22, 2022

Great idea. Note that I'm not using this project myself anymore, so I'm not really actively maintaining it. I'd be happy to merge a PR with proper tests though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants