• safe subseq

    From Bigos@21:1/5 to All on Fri Feb 4 02:28:34 2022
    Why there is no safe version of subseq that with string would return an empty string when outside the string boundaries?

    I have tried following code with strings. Is it correct?

    (defun subseq-safe (sequence start &optional end)
    "Safe subseq SEQUENCE START END makes sure we stay within sequence boundaries"
    (let ((ls (length sequence)))
    (subseq sequence
    (max 0
    (min start
    ls))
    (when end
    (min end
    ls)))))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Spiros Bousbouras@21:1/5 to Bigos on Fri Feb 4 13:49:41 2022
    On Fri, 4 Feb 2022 02:28:34 -0800 (PST)
    Bigos <ruby.object@googlemail.com> wrote:
    Why there is no safe version of subseq that with string would return
    an empty string when outside the string boundaries?

    I have tried following code with strings. Is it correct?

    (defun subseq-safe (sequence start &optional end)
    "Safe subseq SEQUENCE START END makes sure we stay within sequence boundaries"
    (let ((ls (length sequence)))
    (subseq sequence
    (max 0
    (min start
    ls))
    (when end
    (min end
    ls)))))

    (subseq-safe "abcdef" -2) returns the whole string although -2 is outside
    the string boundaries. (subseq-safe "abcdef" 0 -2) enters the debugger.
    So your function does not achieve what you say you want.

    Speaking in general terms , if one wants a subsequence and provides for
    example a negative starting point , chances are higher that there is a
    bug somewhere rather than that they want the negative starting point to silently be turned into 0 before extracting the subsequence. But if
    anyone really wants such a functionality , they can write it easily enough.

    I note also that SUBSEQ works with SETF which subseq-safe doesn't and
    perhaps this is what you prefer.

    --
    vlaho.ninja/prog

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeff Barnett@21:1/5 to Tom Russ on Fri Feb 4 12:12:28 2022
    On 2/4/2022 11:47 AM, Tom Russ wrote:
    On Friday, February 4, 2022 at 5:49:47 AM UTC-8, Spiros Bousbouras wrote:
    On Fri, 4 Feb 2022 02:28:34 -0800 (PST)
    Bigos <ruby....@googlemail.com> wrote:
    Why there is no safe version of subseq that with string would return
    an empty string when outside the string boundaries?

    I have tried following code with strings. Is it correct?

    (defun subseq-safe (sequence start &optional end)
    "Safe subseq SEQUENCE START END makes sure we stay within sequence boundaries"
    (let ((ls (length sequence)))
    (subseq sequence
    (max 0
    (min start
    ls))
    (when end
    (min end
    ls)))))

    Some other observations.
    * This function will still fail, for example with (SUBSEQ-SAFE "0123456789" 5 3).
    * The regular SUBSEQ has the invariant that it *always* returns a sequence of length (end - start)
    * When applied to lists, determining the length of the sequence is an O(n) operation, so a call like
    (SUBSEQ very-long-list 0 5) would have worse performance.
    * +1 to Spiro's observation about the dangers of silently discarding negative indices.

    Is length really an order n operation in most Lisps? I think strings are
    stored as arrays and arrays have headers with length information.
    --
    Jeff Barnett

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tom Russ@21:1/5 to Spiros Bousbouras on Fri Feb 4 10:47:13 2022
    On Friday, February 4, 2022 at 5:49:47 AM UTC-8, Spiros Bousbouras wrote:
    On Fri, 4 Feb 2022 02:28:34 -0800 (PST)
    Bigos <ruby....@googlemail.com> wrote:
    Why there is no safe version of subseq that with string would return
    an empty string when outside the string boundaries?

    I have tried following code with strings. Is it correct?

    (defun subseq-safe (sequence start &optional end)
    "Safe subseq SEQUENCE START END makes sure we stay within sequence boundaries"
    (let ((ls (length sequence)))
    (subseq sequence
    (max 0
    (min start
    ls))
    (when end
    (min end
    ls)))))

    Some other observations.
    * This function will still fail, for example with (SUBSEQ-SAFE "0123456789" 5 3).
    * The regular SUBSEQ has the invariant that it *always* returns a sequence of length (end - start)
    * When applied to lists, determining the length of the sequence is an O(n) operation, so a call like
    (SUBSEQ very-long-list 0 5) would have worse performance.
    * +1 to Spiro's observation about the dangers of silently discarding negative indices.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Spiros Bousbouras@21:1/5 to Jeff Barnett on Fri Feb 4 19:23:53 2022
    On Fri, 4 Feb 2022 12:12:28 -0700
    Jeff Barnett <jbb@notatt.com> wrote:
    On 2/4/2022 11:47 AM, Tom Russ wrote:

    Some other observations.
    * This function will still fail, for example with (SUBSEQ-SAFE "0123456789" 5 3).
    * The regular SUBSEQ has the invariant that it *always* returns a
    sequence of length (end - start)
    * When applied to lists, determining the length of the sequence is an
    ^^^^^^^^^^^^^^^^^^^^^^
    O(n) operation, so a call like
    (SUBSEQ very-long-list 0 5) would have worse performance.

    [...]

    Is length really an order n operation in most Lisps? I think strings are stored as arrays and arrays have headers with length information.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeff Barnett@21:1/5 to Spiros Bousbouras on Fri Feb 4 12:47:01 2022
    On 2/4/2022 12:23 PM, Spiros Bousbouras wrote:
    On Fri, 4 Feb 2022 12:12:28 -0700
    Jeff Barnett <jbb@notatt.com> wrote:
    On 2/4/2022 11:47 AM, Tom Russ wrote:

    Some other observations.
    * This function will still fail, for example with (SUBSEQ-SAFE "0123456789" 5 3).
    * The regular SUBSEQ has the invariant that it *always* returns a
    sequence of length (end - start)
    * When applied to lists, determining the length of the sequence is an
    ^^^^^^^^^^^^^^^^^^^^^^
    O(n) operation, so a call like
    (SUBSEQ very-long-list 0 5) would have worse performance.

    [...]

    Is length really an order n operation in most Lisps? I think strings are
    stored as arrays and arrays have headers with length information.

    Oops!
    --
    Jeff Barnett

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sam Steingold@21:1/5 to All on Mon Feb 7 12:49:15 2022
    * Bigos <ehol.bowrpg@tbbtyrznvy.pbz> [2022-02-04 02:28:34 -0800]:

    Why there is no safe version of subseq that with string would return an empty string when outside the string boundaries?

    I have tried following code with strings. Is it correct?

    (defun subseq-safe (sequence start &optional end)
    "Safe subseq SEQUENCE START END makes sure we stay within sequence boundaries"
    (let ((ls (length sequence)))
    (subseq sequence
    (max 0
    (min start
    ls))
    (when end
    (min end
    ls)))))

    In addition to all the problems others mentioned, this does not seem to
    be _useful_.

    If you are extending `subseq`, it would seem to be more useful for
    "weird" indexes to _add_ functionality, e.g., to treat negative indexes
    as starting from the end (i.e., take start and end modulo length of sequence).

    Cf. slice in python (https://docs.python.org/3/library/functions.html#slice) and `s[i:j:k]` in https://docs.python.org/3/library/stdtypes.html#common-sequence-operations

    --
    Sam Steingold (http://sds.podval.org/) on darwin Ns 10.3.2113 http://childpsy.net http://calmchildstories.com http://steingoldpsychology.com https://thereligionofpeace.com https://jij.org
    Money does not buy happiness, but it helps to make unhappiness comfortable.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)