3 ms·
Maybe it was Dave Long that pointed out that lerp is a generalization of the conditional ternary operator. When the condition is 0, it returns the from-point,
by kragen 2y ago
Maybe it was Dave Long that pointed out that lerp is a generalization of the conditional ternary operator. When the condition is 0, it returns the from-point, and when it's 1, it returns the to-point, but if you generalize it to the whole real number line, you lose nothing semantically except for short-circuiting. Lerp is a pretty common function in graphics—the simplest way to understand Bézier splines is as recursive lerping—but for whatever reason it generally isn't written as an infix operator, which seems to be what Hillel is looking for here.
I think that Hillel's criterion that the ternary operator "can't be decomposed into two sequential binary operators" is too demanding. He says, "bool ? x makes no sense on its own, nor does x : y". Well, that's just because C's grammar is written such that they won't parse. If you extend the grammar to permit them, it's not very hard to extend the semantics as well. For example, you could permit every expression to either succeed, yielding a value, or fail, yielding no value; languages that work in something like this way include regular expressions, META-II, Icon, Unicon, and Prolog, though each of them deviates from it in detail. Then, to get the C/JS semantics, bool ? x succeeds with the value of x if bool is true, or fails if bool is false, and x : y succeeds with the value of x if x succeeds, but if x fails, does y.
You could model the exception mechanism in languages with exceptions as failing to return a value, in which case x : y ends up being a general-purpose exception-handling mechanism, so you can say something like total[k] = (total[k] : 0) + 1 to increment a possibly-nonexistent hash key. Mark Lentczner's language Wheat has an operator like this, spelled !!, which I stole for my own language Bicicleta.
(That's assuming you parse bool ? x : y as (bool ? x) : y. You can also invent semantics that give you the right behavior with the parsing bool ? (x : y) but the ones that occur to me seem less appealing.)
Similarly for Elixir's stepped range 1..10//2. Hillel says, "This isn't decomposable into two binary ops because you can't assign the range to a value and then step the value later." Well, maybe not (I don't know Elixir), but it seems clear enough what r//2 should do if r is the name of a range value: it should produce a new range stepping either by 2 or by twice the previous step.
C++ does something like this for Hillel's (or munificent's) example of a[b] = c: the expression a[b] evaluates to a reference, which in rvalue context decays to an ordinary value, but which can indeed be assigned to a variable. Unfortunately, C++'s reference semantics are somewhat weak, with the consequence that actually overriding operator[] in C++ to return a reference is pretty bug-prone, because you can autovivify things you didn't want to autovivify.
By Hillel's apparent syntactic and decomposability criteria, I think every single two-argument method in Smalltalk is a "ternary operator". 1 to: anInt do: [ :i | r := ( r * self ) ], for example, or arr at: i put: v. As he sort of points out, Objective-C inherited this from its Smalltalk roots, but he sort of throws up his hands at it.
- n_plus_1_acc 2y ago> Hillel says, "This isn't decomposable into two binary ops because you can't assign the range to a value and then step the value later." Well, maybe not (I don't know Elixir), but it seems clear enough what r//2 should do if r is the name of a range value: it should produce a new range stepping either by 2 or by twice the previous step. This is exactly how `1 to 10 by 2` works in scala.
- abecedarius 2y ago> Maybe it was Dave Long that pointed out that lerp is a generalization iirc it was email among the three of us where I brought that up and pointed out that identities about Bézier splines looked pretty with infix lerp notation. (Blurry memory as usual, though.) > to get the C/JS semantics, bool ? x succeeds with the value of x if bool is true, or fails if bool is false, and x : y succeeds with the value of x if x succeeds, but if x fails, does y. There's a potential snag in that if x in (bool ? x) can fail then (true ? x : y) could run y. Of course this can be addressed...
- kragen 2y agoApologies! I didn't mean to steal your valor! My memory is pretty bad these days. My intent with the split between the true/false world of values and the success/failure world was to avoid the situation where a false x would run y, but you're right that if bool ? x can fail (for example if x fails), we haven't really solved anything. And if a failing x doesn't cause bool ? x to fail, we have some other knotty problems: does it return a value, and if so, what? Doesn't that eliminate the possibility of using the failure construct as a model of exceptions? (Is that bad?) It's fun to think about what semantics you'd need to decompose lerp into two such operations. Maybe (a/b) : (c/d) evaluates to (a+c)/(b+d), for example.