GithubHelp home page GithubHelp logo

Comments (7)

boatbomber avatar boatbomber commented on May 8, 2024 5

Potentially pretty valuable (especially if we can get ipairs internally for some extra speed), but the workaround of [val] = true seems good enough for now. This definitely isn't a blocking issue, but could be implemented in the second release!

from fusion.

dphfox avatar dphfox commented on May 8, 2024 2

Ideal for transforming unordered arrays, where order does not matter but duplicates are allowed.

In ForValues, you say this. But doesn't this introduce the initial issue of subtle recalculations?

Nope! The initial issue of subtle recalculations is down to keys being assumed to carry meaning, which they don't in ForValues.

To illustrate:

local array = Value({"Red", "Green", "Blue"})

ComputedPairs(array, function(key, value)
    -- notice ComputedPairs gives you the key
    -- ComputedPairs has to assume the key could be used to calculate the returned value here
    -- therefore, we can't reuse a value if it appears at a different key
    return {
        colour = value
    }
end))

ForValues(array, function(value)
    -- here, ForValues doesn't give us the key
    -- the key could never be used to calculate the returned value here
    -- therefore, we *can* reuse a value if it appears at a different key
    return {
        colour = value
    }
end))

Specifically with respect to duplicate values, the only condition is that two values in the output array never point to the same cached object. ForValues doesn't have to make any further guarantees.

from fusion.

dphfox avatar dphfox commented on May 8, 2024 1

I've decided on an API design for this after much consideration with users.

I plan to replace ComputedPairs with 3 new mapping objects (names being bikeshedded).


ForKeys

Iterates over every pair of the input table. Transforms the key using the processor function (KI) -> KO. Puts transformed key and original value in output table.

Processing only occurs when a key is added. Transformed keys are cached. Values do not trigger processing but are forwarded to the output table directly.

Ideal for transforming sets, where elements are stored as keys.

ForValues

Iterates over every pair of the input table. Transforms the value using the processor function (VI) -> VO. Puts original key and transformed value in output table.

Processing only occurs when a key is added or when a key's value changes to a non-nil value. Transformed values are cached, though care needs to be taken with handling duplicate input values to ensure every value in the output table is unique.

Ideal for transforming unordered arrays, where order does not matter but duplicates are allowed.

ForPairs

Iterates over every pair of the input table. Transforms the pair using the processor function (KI, VI) -> (KO, VO). Puts transformed pair in output table.

Processing only occurs when a key is added or when a key's value changes to a non-nil value. Transformed pairs are cached by key.

Ideal for transforming ordered arrays and dictionaries, where both keys and values carry meaning.


Under this new system there is no direct replacement for ComputedPairs. While this may sound bad, it's actually great - it forces users to consider which behaviour they need, which means Fusion can better optimise for each case without user intervention or cognitive load.

from fusion.

nottirb avatar nottirb commented on May 8, 2024 1

Do we want the optional destructor to be called with the output of For*? Since that's how ComputedPairs currently works:

processor: (K, VI) -> VO,
destructor: (VO) -> ()?

So the idea would be as follows,

For ForKeys:

processor: (KI) -> KO,
destructor: (KO) -> ()?

For ForValues:

processor: (VI) -> VO,
destructor: (VO) -> ()?

For ForPairs:

processor: (KI, VI) -> (KO, VO)
destructor: (KO, VO) -> ()?

from fusion.

Dionysusnu avatar Dionysusnu commented on May 8, 2024 1

If we're on it, this is a good time to implement #97 along with it. But since that's not quite approved yet, maybe this issue should wait a bit.

from fusion.

dphfox avatar dphfox commented on May 8, 2024 1

Yes - I think that the destructor should be forwarded all returned values from the processor function, including any extra metadata the user wishes to provide (see #97).

from fusion.

Dionysusnu avatar Dionysusnu commented on May 8, 2024

Ideal for transforming unordered arrays, where order does not matter but duplicates are allowed.

In ForValues, you say this. But doesn't this introduce the initial issue of subtle recalculations?

from fusion.

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.