Coder Social home page Coder Social logo

Comments (2)

jqmp avatar jqmp commented on July 25, 2024

Hi @ajing, thank you for your question!

First, in general I think it's a good idea for objects to be immutable when possible, because it makes them easier to reason about, harder to make mistakes with, and easier to reuse in large programs. In this particular case it was a harder decision, because a mutable API would be much simpler: we wouldn't have needed a separate FlowBuilder object at all. Still, there were enough concrete benefits to immutability that we thought it was worth it. I'll give two examples.

  1. Suppose you're using a Flow in a notebook like this:
flow.get('result_plot')
flow.setting('preprocessing_type', 'pca').get('result_plot')
flow.setting('ensemble_size', 200).get('result_plot')

If flow were mutable, then each of these cells would be modifying a shared object and their outputs would depend on what order you ran them in. This would be really confusing and annoying! The immutable API lets you keep them separate.

  1. Suppose you want to use a flow as part of another library, like this:
from .prediction_pipeline import prediction_flow

def predict(features, priors=None):
    flow = prediction_flow.setting('features', features)
    if priors is not None:
        flow = flow.setting('priors', priors)
    return flow.get('prediction')

Like before, if the flow object were mutable, then each call to predict might affect the priors value for subsequent calls.

Of course, we could offer both types of API in the same class, or we could allow users to just make a copy of the flow whenever they want to avoid global changes. However, this makes things even more complicated and makes it easier to make mistakes. The Flow/FlowBuilder approach lets you guarantee that no one will modify your flow object, but still lets you switch to a mutable API when necessary. This seemed like a good compromise.

Finally, as you noted, it's also true that allowing mutation would make things like reloading and parallelism more complicated. These weren't the main motivations, but you get a lot of little benefits like this automatically when you make things immutable.

I hope this answered your question! If you have any more questions or feedback, I'd be happy to either talk here or email you directly.

from bionic.

ajing avatar ajing commented on July 25, 2024

Thanks for the clarification! This is very useful.

from bionic.

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.