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

@pipe_util usage #8

Open
galen1090 opened this issue Dec 8, 2019 · 1 comment
Open

@pipe_util usage #8

galen1090 opened this issue Dec 8, 2019 · 1 comment

Comments

@galen1090
Copy link

I'm trying to do some experiments with pipetools:

@pipe_util
def myaggregate(function):
    return partial(functools.reduce, function)

ret = glob.iglob | foreach(readf) | flatten | foreach(len) | myaggregate(operator.add)
print(ret(path))

and it works. But I'm trying to replace flatten with more_itertools.flatten for speed reason:

ret = glob.iglob | foreach(readf) | (more_itertools.flatten) | foreach(len) | myaggregate(operator.add)
print(ret(path))

with more_itertools.flatten takes 6.49 seconds, with flatten 13.68 seconds.
I have tried with:

@pipe_util
def myflatten():
    return partial(more_itertools.flatten)

but it doesn't work, what am I missing?
Thanks

@0101
Copy link
Owner

0101 commented Dec 9, 2019

Hi!

I probably need to describe this better in the documentation. The @pipe_util decorator's purpose is to wrap higher-order functions, which take another function (or something else that can be converted to a function, like X or a regex) as argument. Such as foreach or where. flatten doesn't take any function to specify what it's going to do, so you don't really need pipe_util for anything.

As you can see in the example you provided, just using it directly works fine! You can just import it as flatten or even myflatten and you're done.

The only other feature that @pipe_util gives you, is that you can start the pipe with it, without explicitly prefixing it with pipe | or X |. But you can easily get that like this:

myflatten = X | more_itertools.flatten

Also note that pipetools.flatten functionality is slightly different than the one in more_itertools - it flattens arbitrarily deep nesting and ignores strings and dictionaries.

Also, for your first example, you don't need to explicitly define myaggregate, instead you can just use reduce directly:

... | foreach(len) | (functools.reduce, operator.add)

Or in this particular case even better:

... | foreach(len) | sum

So the main point is that you can just pipe regular functions and you shouldn't really need to use @pipe_util (unless you want to pass X without ~ or have automatic partial application)

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

2 participants