QT4CG-003-02: MK to propose a reformulation of fn:index-of() in terms of fn:index-where()

It seems to be fairly tricky to do this, because (a) we need different comparison rules depending on whether the values are strings or non-strings, and (b) we need to catch the error where values cannot be compared because of their types, without catching other errors.

I considered defining the comparison of values using deep-equal(), but that gives the wrong result for NaN.

I think it would be something like

index-where($input, ->{$item){deep-equal($item, $search) and not (is-NaN($item)))

but that requires careful checking.

It's actually easier to do it the other way around: index-where($input, $predicate) can be expressed as

index-of($input ! $predicate(.), true())

But it might be better to formulate index-where as

for $item at $position in $input
where $predicate($item)
return $position

Michael Kay

Received on Tuesday, 20 September 2022 18:54:23 UTC