Repository navigation
fn:identity: make it variadic #1473
Description
Activity
- addedXQFOAn issue related to Functions and OperatorsAn issue related to Functions and OperatorsFeatureA change that introduces a new featureA change that introduces a new feature
on Oct 1, 2024 I'm not convinced. What should
fn:identity((1,2), (3,4))return, and why? Whatever it returns, the result doesn't seem to be "identical" to the input.fn:voidis another matter. Since that ignores all its arguments, it might as well ignore a variable number of them.I'm not convinced. What should
fn:identity((1,2), (3,4))return, and why? Whatever it returns, the result doesn't seem to be "identical" to the input.A variadic
fn:identitycan be seen as a generalized variant of Haskell’sconstfunction, which takes two arguments and returns only the first one.fn:identitywould do the same: Only the first argument is evaluted, the remaining ones are ignored.#858 (comment) included an example for this. Here is a simplified version:
$insert-withis called with an function argument that decides how to update the value of a map entry. If we hadidentity#2, we could pass it instead offn($a, $b) { $a }:let $insert-with := fn($map, $key, $value, $update-func) { let $old-value := $map($key) let $new-value := if($old-value) then $update-func($value, $old-value) else $value return map:put($map, $key, $new-value) } let $map := { 'foo': 1 } let $summarize := fn($a, $b) { $a + $b } let $replace := fn($a, $b) { $a } return ( $insert-with($map, 'foo', 2, $summarize)('foo'), $insert-with($map, 'foo', 42, $replace)('foo') )
I'd find this very confusing, identity for me, by definition, is not variadic.
In the use case given, doesn't
fn:identity#1serve equally well?In the use case given, doesn't
fn:identity#1serve equally well?I would assume that
identity#1($value, $old-value)raises an error, but I may be wrong?The function coercion rules say If F has lower arity than T, then F is wrapped in a new function that declares and ignores the additional argument, so supplying
identity#1is equivalent to supplyingfn($x, $y){identity#1($x)}.The function coercion rules say If F has lower arity than T, then F is wrapped in a new function that declares and ignores the additional argument, so supplying
identity#1is equivalent to supplyingfn($x, $y){identity#1($x)}.So I got it right.
fn($a, $b) { $a }is probably still easier to read thanfn($x, $y){identity#1($x)}. Iffn:identitywas variadic, though, we could replacefn($a, $b) { $a }withfn:identity#2.While I could see some use for a variadic
fn:identity, it will be hard to teach how it works (the mere existence offn:identityis already a miracle to almost everyone). Maybe it’s better to drop too esoteric endeavors.- addedPropose Closing with no ActionThe issue should be closed with no further discussion and no action (unless anyone objects)The issue should be closed with no further discussion and no action (unless anyone objects)
on Oct 8, 2024 The CG agreed to close this issue without further action at meeting 094.
…see #1437 (comment).