Giter VIP home page Giter VIP logo

Comments (1)

EntilZha avatar EntilZha commented on May 18, 2024

Gave a good shot at implementing this and don't think that the changes to make it happen would be worth it.

Consider this code block:

def f(x):
    if x == 1:
        raise ValueError("Not 1!")
    return x * 2
seq.range(4).map(f)

In order to handle this, its not enough to wrap the code that executes the sequence, it will still error out. In order to catch that exception you either need to catch it where next is called, wrap f when it is passed through to map, or use a yield statement like done here https://github.com/jagill/python-chainz/blob/master/chainz/chain.py#L52

The first one is obviously not an option since thats user code vs library code. There are two possible problematic implementations:

  1. You could wrap f then pass it through. The issue is that there would need to be checks on all methods that accept a function if on_error would work as expected. This is a lot of methods that would have to be changed to support this single one which I don't think adds much value (more on this later)
  2. The second one is clear how to do for map and other functions that operate at a single "level". For example, flat_map since tricky since the results have to get concatenated. The generalization is that implementing this would be method specific so quite a bit of work.

Lastly, I don't think it brings much value for all the effort needed. It would be much easier for user code to handle the exception explicitly. This could be easy to do using flat_map very easily. If there is an error output an empty list, otherwise a tuple of one element (yay monads). It would also be easy to do a map then filter.

I had hoped the implementation wouldn't require so much code change and would be easy, but wasn't to be. I am open to other ideas, but closing issue for now.

from pyfunctional.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.