Re: fn:slice()

I counted ten different optional parameters.

While this function is obviously very powerful, its complexity affects the
learning and confidence of use.

This leads to not-confident / hesitant / unhappy developers.

While a swiss-army knife is the ultimate weapon when nothing else is
available, people often need more consistent, well-thought and comfortable
toolsets that are more focused on particular tasks.

There is a simple law in psychology: the short-term human memory can hold
approximately 7 items:

SimplyPsychology: Short Term Memory
<https://www.simplypsychology.org/short-term-memory.html#:~:text=The%20Magic%20number%207%20(plus,it%20the%20magic%20number%207>

Wikipedia: The Magical Number Seven, Plus or Minus Two
<https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two>

There are even some suggestions about "The magical number 4 in short term
memory":

Cowan, Nelson (2001). "The magical number 4 in short-term memory: A
reconsideration of mental storage capacity". Behavioral and Brain Sciences.
24
<https://www.cambridge.org/core/journals/behavioral-and-brain-sciences/article/magical-number-4-in-shortterm-memory-a-reconsideration-of-mental-storage-capacity/44023F1147D4A1D44BDC0AD226838496>


.HTH,
Dimitre



On Thu, Dec 3, 2020 at 2: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
>
> head(X) ===> slice(X, 1)
> 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").
>


-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Received on Friday, 4 December 2020 02:33:50 UTC