Skip to content

changeAt should be able to take a function #1089

Closed

Description

@neumino
r.expr([1,2,3,4]).changeAt(2, function(doc) { return doc+1})

I would expect to get back [1,2, 4, 4], but now we get

RqlRuntimeError: Expected type DATUM but found FUNCTION:
OPAQUE VALUE FUNCTION in:
r([1, 2, 3, 4]).changeAt(2, function(var_0) { return "var_01"; })
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

We have a work around now with

r.expr([1,2,3,4]).do(function(ar) {
  return ar.changeAt(2, ar.nth(2).add(1))
})

But it's not as good as with a lambda function.

Activity

  1. coffeemug commented on Jun 27, 2013

    @coffeemug
    Contributor

    That would be really nice! Lambdas ftw.

    On Thursday, June 27, 2013, Michel wrote:

    r.expr([1,2,3,4]).changeAt(2, function(doc) { return doc+1})

    I would expect to get back [1,2, 4, 4], but now we get

    RqlRuntimeError: Expected type DATUM but found FUNCTION:
    OPAQUE VALUE FUNCTION in:
    r([1, 2, 3, 4]).changeAt(2, function(var_0) { return "var_01"; })
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    We have a work around now with

    r.expr([1,2,3,4]).do(function(ar) {
    return ar.changeAt(2, ar.nth(2).add(1))
    })

    But it's not as good as with a lambda function.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/1089
    .

  2. jdoliner commented on Jun 28, 2013

    @jdoliner
    Contributor

    Hmm, to me the "workaround" is actually a tad more idiomatic. The code:

    .do{|arr| arr.change_at(2, arr[2] + 1)

    looks fairly similar to:

    arr[2] = arr[2] + 1

    I also do see some cost in the cognitive overhead of adding polymorphism to changeAt and would kind of prefer not to go down the road of adding shortcuts everywhere for things you can accomplish with do however if you guys think this is really a lot better we could do it I suppose.

  3. neumino commented on Jun 28, 2013

    @neumino
    MemberAuthor

    I'm not really a big fan of do (I don't know why though) so I tend to think the syntax with a lambda function would be really nice.

    It doesn't sound anything like urgent to me. Tagging as backlog. We can come back to this later.

  4. jdoliner commented on Jun 28, 2013

    @jdoliner
    Contributor

    Hmm, do is a very powerful part of the language that I feel like we should really try to make accessible to people. I feel like there are going to be several cases like this one and building the logic in to each command is going to be a lot of work and a lot of work for people to learn. Could you think about how we could make do more accessible? I feel like having multiple use cases baked in to every function is going to rapidly lead to an inscrutable language over time even if each individual change seems like it's better than the alternative.

  5. neumino commented on Jun 28, 2013

    @neumino
    MemberAuthor

    Thinking about it, I think one thing I don't really like about do is that when I have a stream, I have to coerce it to an array.
    The syntax here with do is slighly longer too.

    About this case with changeAt, the lambda function is the first thing I tried to replace an element. I was thinking of changeAt like update or replace where you can access the document you update.

  6. coffeemug commented on Jun 28, 2013

    @coffeemug
    Contributor

    I feel like having multiple use cases baked in to every function is going to rapidly lead to an inscrutable language over time even if each individual change seems like it's better than the alternative.

    Hmm, that didn't occur to me, probably because it doesn't feel like a big problem in this case (or other cases where we want to do this). I strongly agree with your sentiment in general, but I think we might want to make an exception for passing lambdas instead of values. We already do this in many cases (e.g. replace, update, probably a bunch of others) and we have at least one more planned that I can think of (contains).

    I think that passing lambdas as a shortcut (or rather, passing a value as a shortcut to lambda) is actually a rather nice technique -- it lets people do advanced things, provides ability to pass value as sugar for simpler cases, and is basically sugar on top of do. It doesn't seem overly confusing, and seems pretty nice in general. I'd be in favor of extending this pattern to commands where it makes sense.

    I do agree that it's a bad idea to have functions that have overly complicated interfaces, though, I just think allowing lambdas is a worthy exception to make to this rule because:

    1. they're so darned useful
    2. they don't seem to be very confusing once you learn the overall principle
    3. it's a really nice pleasant surprise to be able to do it in commands where it makes sense
  7. coffeemug commented on Jun 28, 2013

    @coffeemug
    Contributor

    Thought of one more reason.

    While it complicates the interfaces of functions, it complicates them in a very consistent way. We don't arbitrarily increase complexity of interfaces by making them different for each function; it's the same technique applied to function after function. You do have to understand it once, but it's already ingrained in other functions like update. Once you do understand it, it's really nice to be able to pass it to other functions where it makes sense.

  8. jdoliner commented on Jun 28, 2013

    @jdoliner
    Contributor

    We do this in many cases but none of those other cases can be accomplished so nicely with do. I think it's a really good idea to promote people learning to use the array functions with do as soon as possible because it's just such a powerful combination that lets you write very imperative looking code. I actually think we might want to make our doc examples for these functions use do.

    For example consider the situation where you want to pop an element off the front of an array and push it on to the back, or delete the first element of an array if it's equal to a certain value. These are all very intuitive to write once you understand do and I think they're actually the type of operation that are going to be very common.

    I'm not really opposed to this feature in and of itself. It's just that it feels like it's baking the sentiment of "do is a workaround" in to our language which is a direction I really don't like." And even though these are small changes that are fairly unified I do feel like things like this actually have a big impact on the feel of a language. When a language consistently introduces me to concepts which I get to keep applying that's when a language really starts to feel powerful to me and I think promoting the do syntax as the way to do things like this is going to lead to that more than the alternative.

  9. coffeemug commented on Jun 28, 2013

    @coffeemug
    Contributor

    I really think of this lambda API as nice sugar for the slightly more complex do syntax. But I see what you're saying. (I'm not sure what to do here, I have to write some queries and think about it a little bit)

  10. jdoliner commented on Jun 28, 2013

    @jdoliner
    Contributor

    Yeah I think this particular syntax might be fine. If we get to the point where like, delete_at is taking a predicate. And splice_at is taking a function from array to array we might want to reevaluate.

  11. coffeemug commented on Jun 28, 2013

    @coffeemug
    Contributor

    The most useful lambda for things like delete_at and splice_at would probably search for elements, which would be really weird. I'd definitely be opposed to that.

  12. danielmewes commented on Jul 1, 2013

    @danielmewes
    Member

    We can also see it like this: Once the majority of functions that take a datum can also take a lambda function, the complexity of the interface actually decreases with every additional function that accepts lambdas instead of datums as well.

    Once every function accepts both, the complexity is arguably the same as if none accepts both.

    Complexity used here in its information-theoretical meaning... However I would argue that in this case that definition of complexity meets the subjective complexity of the interface pretty well.

  13. thelinuxlich commented on Mar 26, 2018

    @thelinuxlich

    @srh close this in favor of #4236

  14. modified the milestones: backlog, duplicate on Mar 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions