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

thread_as function ?? #540

Open
gkspranger opened this issue Jul 26, 2022 · 1 comment
Open

thread_as function ?? #540

gkspranger opened this issue Jul 26, 2022 · 1 comment

Comments

@gkspranger
Copy link

is there any way to achieve a "thread as" experience like there is in Clojure ??

https://clojuredocs.org/clojure.core/as-%3E

my goal is to be able to use the result from the previous evaluation, anywhere in the current evaluation .. i get that the "variable" declaration (v) could conflict with future args, but if i am allowed to "name" my variable and it is unique enough -- i could reasonably assume my positional arguments would be safe from conflicts ..

for example, i would hope to be able to do something like this ::

v = f"{uuid.uuid4()}some_variable_name_that_wont_collide_with_future_derived_vals"
def add(x, y): return x + y
def pow(x, y): return x**y

thread_as(
    1, v,
    (add, 4, v),
    (pow, v, 2),
)
# 25

if there is another way to achieve it -- i would love to hear it ..

thanks for the help !!

@gkspranger
Copy link
Author

just in case there is no current way to do this, i tried hacking at it and came up with the solution below (WARNING: non-expert-pythoner) .. so it does seem possible -- but again, if there is an existing way to accomplish this, i would like to know about it ..

thanks again

from functools import partial, reduce
import uuid


v = f"{uuid.uuid4()}myspecialvar"
def add(x, y): return x + y
def pow(x, y): return x**y

def thread_as(val, v, *forms):
    def to_as_variable(val, arg):
        return val if arg == v else arg

    def evalform_as(val, form):
        if isinstance(form, tuple):
            func, args = form[0], form[1:]
            return func(*map(partial(to_as_variable, val), args))
    return reduce(evalform_as, forms, val)

print(
    thread_as(
        1, v,
        (add, 4, v),
        (pow, v, 2),
    )
)

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

No branches or pull requests

1 participant