[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?
From: |
Bastien |
Subject: |
Re: Why (substring "abc" 0 4) does not return "abc" instead of an error? |
Date: |
Mon, 16 Jul 2012 21:25:16 +0200 |
User-agent: |
Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.1.50 (gnu/linux) |
Hi Tassilo,
Tassilo Horn <address@hidden> writes:
> Bastien <address@hidden> writes:
>
>>> We have general functionality when you want to ignore some errors,
>>> such as condition-case.
>>
>> Also, I'm fine with
>>
>> (substring "abc" -1 1)
>> => #ERROR
>
> I don't see why that justifies an error and (substring "abc" 0 4) does
> not. -1 is a valid FROM index meaning the length of the string minus
> one. Its just that the TO index is smaller than FROM here, but IMO
> that's the same class of errors as a too large TO index.
I would distinguish problems caused by only one indice from those
caused by the relationship between two indices. Just nit-picking.
>> so using ̀€condition-case' would not help me distinguish
>> between the case above and (substring "abc" 0 4), which
>> is what I want.
>
> A condition-case handler has access to the args given to the erroring
> form, so the cases could be distinguished although both signal
> args-of-range. Well, not that it would help you much here.
Fair enough.
>> I see the benefit of having
>>
>> (substring "abc" 0 4)
>> => "abc"
>>
>> in terms of simplifying Elisp writing -- and I still fail
>> to see the harm (but maybe Pascal will tell me where he has
>> been bitten by this.)
>
> In my experiences, out-of-range indices into strings or arrays are
> almost always programming errors. I'm not even able to come up with
> some concrete use-case where I'd like to have the suggested behavior.
> Either I know exactly what I'm operating on and use indices, or I have
> only some general assumptions and then use more fuzzy things like
> splitting by regular expressions.
I get your point about programming errors. And I think it's just
part of my brain that cannot help thinking of the third argument as
a _length_ instead of an _index_. I now see how allowing to fail
quietly would make this misconception more common.
How about these two defun-future-defsubst candidates?
(defun string-head (string n)
"Return N characters starting from the beginning of STRING.
If N is larger than the length of STRING, return it."
(substring string 0 (min n (length string))))
(defun string-tail (string n)
"Return N characters starting from the end of STRING.
If N is larger than the length of STRING, return it."
(let* ((l (length string))
(s (- l n))
(d (if (> s 0) s 0)))
(substring string d l)))
--
Bastien
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, (continued)
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Juanma Barranquero, 2012/07/15
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Bastien, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Stefan Monnier, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Bastien, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Bastien, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Tassilo Horn, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Lars Magne Ingebrigtsen, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Bastien, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Tassilo Horn, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Pascal J. Bourguignon, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?,
Bastien <=
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Bastien, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Pascal J. Bourguignon, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Stefan Monnier, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Lennart Borgman, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Bastien, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Lennart Borgman, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Andreas Schwab, 2012/07/16
- Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?, Juanma Barranquero, 2012/07/16