backspaces / agentscript

An Agent Based Modeling system (ABM)
http://backspaces.github.io/agentscript/
GNU General Public License v3.0
107 stars 14 forks source link

oops missing reduce starting val #36

Closed bennlich closed 3 years ago

backspaces commented 3 years ago

Makes sense, empty arrays return 0. But maybe better would be to check for empty and either throw an error or return null or nan +/- infinity.

But throwing is likely silly and 0 seems reasonable

backspaces commented 3 years ago

Whoa, just thought a bit about it. Having min start at 0 is likely a problem, right? Maybe min should start at +Infinity and max at -Infinity. ... and with a warning.

backspaces commented 3 years ago

I just switched to this:

    min(key) {
        return this.reduce(
            (prev, o) => Math.min(prev, key ? o[key] : o),
            Infinity
        )
    }
    max(key) {
        return this.reduce(
            (prev, o) => Math.max(prev, key ? o[key] : o),
            -Infinity
        )
    }

What do you think?

bennlich commented 3 years ago

Nice! Good catch.