Skip to content

Single argument r.sub #1184

Closed
Closed

Description

@AtnNn

I tried r.sub(1) expecting -1, but I got an RqlDriverError instead:

RqlDriverError: Expected 2 or more argument(s) but found 1.

Activity

  1. jdoliner commented on Jul 18, 2013

    @jdoliner
    Contributor

    This issue is just a story about something you did. Issues should be a clear description of a problem and what you think should be done to fix it.

  2. AtnNn commented on Jul 18, 2013

    @AtnNn
    MemberAuthor

    Thanks @jdoliner. I beleive the issue is that the javascript driver is constructing an invalid query and should be fixed.

  3. neumino commented on Jul 18, 2013

    @neumino
    Member

    I may be wrong, but while r.sub(1) could work in javascript, -r.expr(1) cannot work in Python and Ruby.

  4. wmrowan commented on Jul 18, 2013

    @wmrowan
    Contributor

    The javascript driver is not constructing an invalid query, the ReQL api is being incorrectly used and the error message received is the expected error message given this incorrect usage. r.sub(1,2) is binary subtraction, equivalent 1 - 2. We don't currently support unary negation (i.e. -1) which is what @AtnNn appears to have been trying to do.

    This issue should probably be named "ReQL Proposal: unary negation". Doing this would require a new ReQL term type (NEGATE probably) or overloading SUB to mean negation when given only 1 argument.

    Regardless of which we choose, we can implement it in the JS driver side by overloading r.sub on 1 argument (question, do we want r.expr(1).sub() to do this?).

    In Python and Ruby it will be a bit different because we have to overload the appropriate operator. In Python this would mean overloading __neg__.

    One last thought, if we did add negation, how do we feel about adding r.abs for absolute value?

  5. jdoliner commented on Jul 18, 2013

    @jdoliner
    Contributor

    Unary is unfortunately not overloadable in python. I'm not sure about in ruby either. I'm a little bit dubious that r.sub(1) should return -1. I think it's a lot more likely that r.sub(1) is a typo than an attempt to negate a number and I'd like to leave the option open to have r.sub(1) be shorthand for lambda x: 1 - x in the future.

  6. wmrowan commented on Jul 18, 2013

    @wmrowan
    Contributor

    @jdoliner I do believe you can overload __neg__(self) in Python to implement this. Additionally, having r.sub(1) be shorthand for function(x) { return 1 - x} seems quite unintuitive to me. Still, I don't necessarily think we should support unary r.sub. The usefulness of this seems quite limited.

  7. coffeemug commented on Jul 18, 2013

    @coffeemug
    Contributor

    I think the current behavior is fine. We could probably debate this for a while and get the product to behave slightly better, but I think it would be in the realm of diminishing returns.

    What does everyone else think? (I'd like to close this issue unless people feel strongly that we should fix this)

  8. jdoliner commented on Jul 18, 2013

    @jdoliner
    Contributor

    @wmrowan It appears you can overload __neg__ I'm not sure why we didn't do that.

    Having r.sub(1) be shorthand for is probably unintuitive by itself but if we had currying in other places in the language it could wind up being really awesome. You could writing things like:

    table.map({"foo" : r.sub(1), "bar" : r.add(1)})

    anyways, this is outside of the scope of this discussion.

    @coffeemug definitely close this. It's a nitty gritty issue that no real users have run in to and I see no reason to believe it's actually going to make things appreciably better.

  9. coffeemug commented on Jul 18, 2013

    @coffeemug
    Contributor

    Ok.

    Also, I think that even if real users end up running into this, it's the kind of feature where we can confidently say no. I don't think anyone would stop using the product because this is missing, and I feel like people would understand our rationale for not taking this on in the near future.

    Closing.

  10. mlucy commented on Jul 18, 2013

    @mlucy
    Member

    I realize I'm late to the party, but I think I agree with @AtnNn here.

    We already support prefix negation in Ruby. We translate prefix minus of x to 0-x. I don't see any reason why we shouldn't support this behavior in Python as well; it's absurdly easy for us to do (literally one line in func.rb) and makes life slightly better for users.

    I also think that we should strongly consider accepting 1-ary subtraction on the server. Every language I know of that offers n-ary subtraction (mostly just the lisps) accepts 1-ary subtraction. For instance, in Common Lisp, (- 2) gives -2.

    The question for small issues like this isn't "will more people use the product if we do this", but "will more people use the product if we consistently take the time to get small issues like this right". I think the answer to the second one is an emphatic YES.

    I'm gonna open up a separate ReQL proposal to debate whether to have 1-ary arithmetic operators, but leave it in backlog because I think we have more important ReQL problems in the 1.8 pipeline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions