dbrattli / Expression

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

Support for Either type #63

Closed philvarner closed 2 years ago

philvarner commented 2 years ago

Is your feature request related to a problem? Please describe.

Coming from a bit of Haskell and more Scala, I was surprised to see there was no Either type. I'd like to see this added.

Describe the solution you'd like

I'd like to see the library have an Either type.

Describe alternatives you've considered

I'm aware that this library is supposed to be a more direct implementation of F# in Python than every type from other languages. Either is a pretty common concept (e.g., in Haskell and Scala), even if in Scala, Try is more frequently used. A lot of uses of Either will use Result or Try instead, but I think it would be good to have a generic Either for completeness, even if it's not part of F#.

Additional context

n/a

dbrattli commented 2 years ago

Hi, I the only difference between Either and Result would be just the naming i.e Ok = Left, Error = Right, or am I missing something? What would Either do compared to Result? There is btw a choice type in https://github.com/cognitedata/Expression/blob/main/expression/core/choice.py but it hasn't that many helper functions as Result. This is the same for F#

philvarner commented 2 years ago

I think this was my misunderstanding of the typing of Result -- the use of the word Error in the typing it made me think that it had to be an Exception. I've been switching between JS and Python, and forgot Python doesn't actually have a type Error even though there are a lot of Exception classes named as such.

I think more documentation would be useful on how to use Result. This is such a great library, and far less known than it should be. I think what would help for the documentation is having guides showing how to move from another language with similar concepts. A lot of the docs are F# specific or are explaining functional concepts from the ground-up. I'd be interested in writing some "migration guides" for using Expression if you're coming from Scala, Haskell, Rust, etc., since I think most people who are going to use this library will be coming from some other functional language but need to use Python now.

Closing.