Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

878 Proposed extension to subsequence #874

Closed
wants to merge 1 commit into from

Conversation

michaelhkay
Copy link
Contributor

Following discussion under issue #844, I decided to explore the possibility of extending subsequence() with optional parameters, with the aim of making the quartet of items-before/after/starting-with/ending-with unnecessary.

This is the spec that results. I feel it's a good trade-off; by adding three optional parameters to fn:subsequence, we can eliminate 4 functions that we are having trouble finding names for. The examples feel to me to be intuitive and readable; and there is more capability in the new function than we had before, for example by combining a predicate for the start position with an integer for the length.

I haven't explored arity-2 callbacks - these certainly need some notes and examples.

@ChristianGruen ChristianGruen changed the title Proposed extension to subsequence 878: Proposed extension to subsequence Dec 6, 2023
@ChristianGruen ChristianGruen changed the title 878: Proposed extension to subsequence 878 Proposed extension to subsequence Dec 6, 2023
<code>$input</code> whose 1-based position
is greater than or equal to <code>$start</code> (rounded to an integer).
No error occurs if <code>$start</code> is zero or negative.</p>

<p>In the three-argument case, The function returns a sequence comprising those items of
<p>In the case where <code>$input</code>, <code>$start</code>, and <code>$length</code>
are supplied, he function returns a sequence comprising those items of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
are supplied, he function returns a sequence comprising those items of
are supplied, the function returns a sequence comprising those items of

@ChristianGruen
Copy link
Contributor

We should probably raise an error…

  • if both $start and $from are supplied,
  • if more than one of $length, $while, or $until is supplied, or
  • optionally, if none of the 5 arguments is supplied (unless we want to allow fn:subsequence($seq) returning $seq).

@michaelhkay
Copy link
Contributor Author

Yes, we should add specific error conditions for invalid combinations of arguments.

Yes, it is intended that if start and length are both defaulted, you get the whole sequence back. It's not a useful combination of options, but since it falls out naturally, there's no point disallowing it.

Copy link
Contributor

@ChristianGruen ChristianGruen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. One or two examples and an explanatory note – or some equivalent code – could be added for the positional argument ($pos).

@ChristianGruen ChristianGruen added the Tests Needed Tests need to be written or merged label Dec 20, 2023
@michaelhkay
Copy link
Contributor Author

For an interesting use case where the new function might be convenient, see

https://stackoverflow.com/questions/77724793

Copy link
Contributor

@ChristianGruen ChristianGruen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My summarized comments:

  1. We should raise an error…
  • if both $start and $from are supplied, or
  • if more than one of $length, $while, or $until is supplied.
  1. One or two examples and an explanatory note – or some equivalent code – should be added for the positional argument ($pos).

@michaelhkay
Copy link
Contributor Author

michaelhkay commented Jan 9, 2024

Notes from discussion on 9th Jan. There was a feeling the proposed function was too complex. I agreed to explore the idea of a simpler subsequence-where() function.

Perhaps subsequence-where($input, $from as predicate := true#0, $to as predicate := false#0), with the semantics being something like

let $start := index-where($input, $from)[1] otherwise number('+INF')
let $tail := subsequence($input, $start)
let $end := index-where($tail, $to)[1] otherwise number('+INF')
return subsequence($tail, 1, $end)

@ChristianGruen
Copy link
Contributor

Perhaps subsequence-where($input, $from as predicate := true#0, $to as predicate := false#0), with the semantics being something like

Related: #844 (comment)

@ChristianGruen
Copy link
Contributor

Dropped, in favor of #940.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tests Needed Tests need to be written or merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants