Search code examples
rebolrebol3

In a series! what is the best way of removing the last element


What is the most succinct way of removing the last element in a Rebol series?

Options I have found so far are

s: "abc"
head remove back tail s

and

s: "abc"
take/last s

Solution

  • Here I wrote a REMOVE-LAST function,

    remove-last: func [
        "Removes value(s) from tail of a series."
        series [series! port! bitset! none!]
        /part range [number!] "Removes to a given length."
    ] [
        either part [
            clear skip tail series negate range
        ] [
            remove back tail series
        ]
    ]
    

    Example use:

    b: [a b c d e f g]
    remove-last b ;== [], 'g removed, tail of the series return.
    head remove-last/part b 2 ;== [a b c d], 'e and 'f removed
    

    It returns the tail of the series to be able to use in following situation;

    b: [a b c d e f g]
    head insert remove-last b 'x  ;== [a b c d e f x]