# fn:slice()

• From: Michael Kay <mike@saxonica.com>
• Date: Thu, 3 Dec 2020 10:27:10 +0000
• Message-Id: <0DED7274-AA32-454B-9CFB-B8280E5A1550@saxonica.com>
```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 10:27:26 UTC