JulienPalard / Pipe

A Python library to use infix notation in Python
MIT License
1.96k stars 113 forks source link

Lambda replacements #60

Closed gamis closed 2 years ago

gamis commented 3 years ago

Hi!

I like this package! I wrote something similar: https://github.com/gamis/flo, but I think I like yours better.

The one thing mine has that yours doesn't is a concise replacement for lambda functions. I find lambda x: x**2 kind of annoying. With my library, instead of having code that looks like

mylist = ['pretty','cool','items', 'kiddo']
myindex = mylist | map(lambda x: x.upper()) | where(lambda x: 'E' in x) | groupby(key=lambda x: x[0]) | select(lambda x: x[0])

it could look like

myindex = mylist | map( _.upper() ) | where( _.has('E') ) | groupby( _[0] ) | select( _[0] )

or potentially even terser:

myindex = mylist | map_.upper() | where_.has('E') | groupby_[0] | select_[0]

If you're interested in such an addition, I can work on a PR in the next month or two. In any case, I'd love any feedback you have.

Thanks!

Greg

JulienPalard commented 3 years ago

First impression: If _.has('E') generates a lambda like lambda x: 'E' in x, it could be packaged as its own, and used in conjunction with Pipe, but also many other contexts, would'nt this be better?

I would avoid using _, it's already used by unused variables, gettext, and match wildcard, that's already too much.

I would name it it.

Like:

it.has("E")

What do you think?

Playing around the idea:

from pipe import *
import it

mylist = ['pretty','cool','items', 'kiddo']
myindex = mylist | map(it.upper()) | where(it.has("E")) | groupby(key=it[0]) | select(it[0])
gamis commented 3 years ago

Thanks for the fast reply!

Yes, it could be a separate package but I think it would make Pipe more compelling to have it included. Maybe the two packages could at least reference each other in their documentation.

As for the underscore notation, yes, i agree completely. In practice I use e_, short for “each” but the package lets you use whatever you want. I often use “it” to mean “iterator”.

—- Sent from my phone


From: Julien Palard @.> Sent: Friday, November 12, 2021 4:17:27 PM To: JulienPalard/Pipe @.> Cc: Greg Amis @.>; Author @.> Subject: Re: [JulienPalard/Pipe] Lambda replacements (Issue #60)

First impression: If _.has('E') generates a lambda like lambda x: 'E' in x, it could be packaged as its own, and used in conjunction with Pipe, but also many other contexts, would'nt this be better?

I would avoid using _, it's already used by unused variables, gettext, and match wildcard, that's already too much.

I would name it it.

Like:

it.has("E")

What do you think?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/JulienPalard/Pipe/issues/60#issuecomment-967573724, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AB6CHLBYV4XXBK73BEIJ65DULV76PANCNFSM5H5S4VNQ. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

JulienPalard commented 3 years ago

Yes, cross-linking the docs with some examples make sense, but I don't think people using it should have to pull Pipe nor the other way around.

JulienPalard commented 2 years ago

I'm closing the issue, feel free to open a PR if you're maintaining your project and would like a back-reference from pipe's README to you project.