dbrattli / Expression

Pragmatic functional programming for Python inspired by F#
https://expression.readthedocs.io
MIT License
424 stars 31 forks source link

Difference to dry-python/returns #48

Closed jim108dev closed 2 months ago

jim108dev commented 2 years ago

Hi! Thank you for creating this repo. I was wondering what exactly are the pros and cons opposite to https://github.com/dry-python/returns. Maybe someone could make this more clear in the Readme.

Thank you!

dbrattli commented 2 years ago

Thanks for the input. Returns looks like a great and well made library for Python. This library (Expression) tries to stay close to F# abstractions and naming, i.e by using Expression you will learn some F# as well. But I don't think we should try to compare ourselves with others. I really don't know Returns and have never used it myself.

jim108dev commented 2 years ago

Hi @dbrattli, thanks for the reply. What I really do a lot lately is piping with pandas and it is not type safe with pylance.,e. g.

df_train_features = (df_train_source.pipe(filter_value, "Month", 9)
   .pipe(filter_columns, FEATURES)
   .pipe(dropna)
)

Does Expression offer a solution for that?

dbrattli commented 2 years ago

Yes, I think so. But you would need to craft your handlers so that you can partially apply them (when they need multiple arguments) e.g:

Screenshot 2021-11-11 at 07 28 48
jim108dev commented 2 years ago

I see. On the readme it says: "Avoid currying, not supported in Python by default and not a well known concept by Python programmers." Does this mean I have to write inner functions for most of my functions now?

dbrattli commented 2 years ago

Maybe. The problem with currying is that the function also looses it's type. But there's perhaps another solution. Let me check ...

dbrattli commented 2 years ago

Well, piping only works with functions taking a single argument, so we need to turn the handlers into single argument functions. Currying or applying a function partially is not really the normal way of programming in Python, but e.g. decorations are commonly used (returning inner functions). So a few non-magic decorators could do the trick. I'm considering adding these decorators #49 that should hopefully do the trick.

phi-friday commented 1 year ago

The biggest difference I think is that expression supports pylance, but returns only supports mypy.