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
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"; })
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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"; })
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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.
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.
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.
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:
they're so darned useful
they don't seem to be very confusing once you learn the overall principle
it's a really nice pleasant surprise to be able to do it in commands where it makes sense
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.
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.
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)
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.
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.
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.
Activity
coffeemug commentedon Jun 27, 2013
That would be really nice! Lambdas ftw.
On Thursday, June 27, 2013, Michel wrote:
jdoliner commentedon Jun 28, 2013
Hmm, to me the "workaround" is actually a tad more idiomatic. The code:
looks fairly similar to:
I also do see some cost in the cognitive overhead of adding polymorphism to
changeAtand would kind of prefer not to go down the road of adding shortcuts everywhere for things you can accomplish withdohowever if you guys think this is really a lot better we could do it I suppose.neumino commentedon Jun 28, 2013
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.
jdoliner commentedon Jun 28, 2013
Hmm,
dois 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 makedomore 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.neumino commentedon Jun 28, 2013
Thinking about it, I think one thing I don't really like about
dois that when I have a stream, I have to coerce it to an array.The syntax here with
dois 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
changeAtlikeupdateorreplacewhere you can access the document you update.coffeemug commentedon Jun 28, 2013
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:
coffeemug commentedon Jun 28, 2013
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.jdoliner commentedon Jun 28, 2013
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 withdoas 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 usedo.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
doand 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 "
dois 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 thedosyntax as the way to do things like this is going to lead to that more than the alternative.coffeemug commentedon Jun 28, 2013
I really think of this lambda API as nice sugar for the slightly more complex
dosyntax. 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)jdoliner commentedon Jun 28, 2013
Yeah I think this particular syntax might be fine. If we get to the point where like,
delete_atis taking a predicate. Andsplice_atis taking a function from array to array we might want to reevaluate.coffeemug commentedon Jun 28, 2013
The most useful lambda for things like
delete_atandsplice_atwould probably search for elements, which would be really weird. I'd definitely be opposed to that.danielmewes commentedon Jul 1, 2013
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.
thelinuxlich commentedon Mar 26, 2018
@srh close this in favor of #4236