Skip to content

Extend changeAt to accept functions in both arguments. #4236

Open

Description

@thejsj

Currently, it's very cumbersome to alter arrays. Usually, they involved finding the index of a particular value and then using changesAt with the index to update the value.

After talking to @danielmewes, we had the idea of using changesAt to do this kind of operation by being able to take a function in the first and second argument. The first argument would work as a filter and the second argument would return a value.

Example:

r.expr([ { name: 'jorge', score: 100 }, { name: 'atnnn', score: 40 }])
  .changeAt(function (value) { 
    return value('name').eq('jorge')' 
   }, function (value) { 
      return { name: value('name'), score: 99999 }'; 
   });

This would be consistent with the current API, because you can do something like:

r.expr([ { name: 'jorge', score: 100 }, { name: 'atnnn', score: 40 }])
  .changeAt(1, function (value) { 
      return { name: value('name'), score: 99999 }'; 
   });

or

r.expr([ { name: 'jorge', score: 100 }, { name: 'atnnn', score: 40 }])
  .changeAt(function (value) { 
    return value('name').eq('jorge')' 
   }, "Hulk");

Activity

  1. changed the title [-]Extend `changesAt` to accept functions in both arguments.[/-] [+]Extend `changeAt` to accept functions in both arguments.[/+] on May 15, 2015
  2. larkost commented on May 15, 2015

    @larkost
    Collaborator

    I think that rather than have two functions it would be better to have just a single function that would return the new (possibly unchanged) value. There is not likely to be much performance difference, and in some degenerate cases (change all values to a constant) it eliminates some redundancy.

    Additionally I think and any functions in this should be provided with the current index as well. Most of the time people are not going to care, but there is going to be some case...

  3. danielmewes commented on May 15, 2015

    @danielmewes
    Member

    I think the difference is that if we use two arguments, you can avoid using a function in many cases.
    For example if you want to replace every occurrence of "a" by "b" in an array, you could just write
    r.expr(["a", "b", "c", "d"]).changeAt("a", "b") -> ["b", "b", "c", "d"].

    The single function case is already covered by map actually.

    👍 for passing the index to the function as well.

    Unfortunately now that I'm thinking more about this, it turns out that that the function-less syntax is ambiguous if the thing you want to replace is a number. In that case we cannot distinguish changeAt(2, 3) meaning replace every occurrence of the number 2 by 3, from changeAt(2, 3) meaning replace the element at index 2 by 3.

    This makes it look like a much worse idea to me. We could introduce a new term for this operation.
    However #2692 at this point seems far superior.

  4. added this to the subsequent milestone on May 15, 2015
  5. mlucy commented on May 15, 2015

    @mlucy
    Member

    @thejsj -- instead of arr.change_at(lambda row: row.func1, lambda row: row.func2) you can do arr.map(lambda row: r.branch(row.func1, row.func2, row)).

  6. deontologician commented on Jun 11, 2015

    @deontologician
    Contributor

    +1 working with admin api today and could have used this

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions