You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Extend changeAt to accept functions in both arguments. #4236
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.
changed the title [-]Extend `changesAt` to accept functions in both arguments.[/-][+]Extend `changeAt` to accept functions in both arguments.[/+]on May 15, 2015
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...
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.
Activity
[-]Extend `changesAt` to accept functions in both arguments.[/-][+]Extend `changeAt` to accept functions in both arguments.[/+]larkost commentedon May 15, 2015
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...
danielmewes commentedon May 15, 2015
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 writer.expr(["a", "b", "c", "d"]).changeAt("a", "b")->["b", "b", "c", "d"].The single function case is already covered by
mapactually.👍 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, fromchangeAt(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.
mlucy commentedon May 15, 2015
@thejsj -- instead of
arr.change_at(lambda row: row.func1, lambda row: row.func2)you can doarr.map(lambda row: r.branch(row.func1, row.func2, row)).deontologician commentedon Jun 11, 2015
+1 working with admin api today and could have used this