# Re: fn:slice()

• From: Christian Grün <cg@basex.org>
• Date: Thu, 3 Dec 2020 17:27:54 +0100
• To: Michael Kay <mike@saxonica.com>
```Definitely a powerful concept; thanks for the full list and the
examples. I think that "slice" as function name is well-known enough
in other communities.

I hope that things won’t get too bewildering once we consider all the
possible combinations (end/length; step/length;
before/aftter/from/until; …).
____________________________________

On Thu, Dec 3, 2020 at 11:27 AM Michael Kay <mike@saxonica.com> wrote:
>
> Here's my latest attempt, illustrated by example:
>
> Note that this (potentially) replaces existing or proposed functions as follows
>
> tail(X) ===> slice(X, start:=2)
> item-at(X, N) ===> slice(X, N)
> foot(X) ===> slice(X, -1)
> truncate(X) ===> slice(X, end:=-2)
> slice(X, A to B by C) ===> slice(X, start:=A, end:=B, step:=C)
> subsequence(X, S, L) ==> slice(X, start:=S, length:=L)
> items-before(X, P) ===> slice(X, before:=P)
> items-after(X, P) ===> slice(X, after:=P)
> items-from(X, P) ===> slice(X, from:=P)
> items-until(X, P) ===> slice(X, until:=P)
> X[position() gt P] ===> slice(X, start:=P+1)
> X[position() = A to B] ===> slice(X, start:=A, end:=B)
> X[last()] ===> slice(X, -1)
> X[last()-1] ===> slice)X, -2)
>
> All variants return the results in their original order.
>
> The expression fn:slice(("a", "b", "c", "d", "e"), at:=2) returns "b".
>
> The expression fn:slice(("a", "b", "c", "d", "e"), at:=-1) returns "e".
>
> The expression fn:slice(("a", "b", "c", "d", "e"), at:=0) returns ().
>
> The expression fn:slice(("a", "b", "c", "d", "e"), start:=2) returns ("b", "c", "d", "e").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), start:=-2) returns ("d", "e").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), step:=2) returns ("a", "c", "e").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), start:=2, step:=2) returns ("b", "d").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), end:=3) returns ("a", "b", "c").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), start:=2, end:=3) returns ("b", "c").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), start:=2, length:=3) returns ("b", "c", "d").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), start:=-3, length:=2) returns ("c", "d").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), end:=-2) returns ("a", "b", "c", "d").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), at:=(1, 4, 5)) returns ("a", "d", "e").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), at:=(5, 4, 1)) returns ("a", "d", "e").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), at:=(1, -1)) returns ("a", "e").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), at:=(1 to 4) returns ("a", "b", "c", "d").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), from:=matches(?, "c")) returns ("c", "d", "e").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), after:=matches(?, "c")) returns ("d", "e").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), before:=matches(?, "c")) returns ("a", "b").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), until:=matches(?, "c")) returns ("a", "b", "c").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), start:=2, until:=matches(?, "c"))returns ("b", "c").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), from:=matches(?, "c"), until:=matches(?, "e")) returns ("c", "d", "e").
>
> The expression fn:slice(("a", "b", "c", "d", "e"), start:=2, from:=matches(?, "a")) returns ("b", "c", "d", "e").
```

Received on Thursday, 3 December 2020 16:28:19 UTC