On Fri, Feb 8, 2013 at 2:23 AM, Michele La Monaca < [email protected]> wrote:
> On Thu, Feb 7, 2013 at 12:20 AM, Alex Shinn <[email protected]> wrote: > > Hi, > > > > On Thu, Feb 7, 2013 at 3:20 AM, Michele La Monaca > > <[email protected]> wrote: > >> > >> [..] I don't think > >> > >> (substring2 "foo" 0 10) -> "foo" > >> > >> is conceptually wrong or sloppy as long as you know exactly what the > >> semantic of the function is ("give me at most N chars" -> perl or > >> "give me chars up to the Nth position or up to the end of the string > >> whatever the first" -> python, ruby). > > > > > > Truncating a string to a maximum length is a common > > operation for formatting - I was surprised there was no > > easy way to do this with SRFI-13. But in this case the > > start argument isn't needed and distracting. You probably > > want something like: > > Well, no. If I wanted a truncate function I would have asked for it. I > used 0 as START to simplify the exposition (in that case the END > parameter can be interpreted as length or position indifferently). > You didn't use it just to simplify, because the task you described in English was "give me at most N chars." This is a common task, and always has a START of 0. There may be cases in Python where you first want to take the substring from START to the end of the string, and then want to truncate the result to END-START chars. This becomes an idiom in Python, so it seems natural to you, but there is always a specific reason you want to truncate separate from the substring you are taking. In Scheme this becomes (string-truncate (string-copy str start) max-len) You could combine these two functions (slice str start (- max-len start)) but this hides what you're actually doing and likely not as common, so is probably a bad idiom in Scheme. It doesn't make sense to provide such artificially combined functions. For example, you may notice sometimes you want to take a substring and then lowercase the result, but that doesn't mean it's worth defining a substring-downcase function. To add to the earlier rants abouts the dangers of DWIM behavior, I had a nasty bug in one of my Python programs the other day. We have an API for tasks which produce file outputs, and for convenience if there's only a single output we just return that, otherwise we return a tuple of strings. I had a task where I wanted to copy the first result to a separate directory, and this was a task that returned a tuple, so I used outputs[0]. Unfortunately, after some refactoring this became a task which only returned one output, so outputs became a string and outputs[0] was "/"! The program was still perfectly valid, and gave me the command-line: cp -R / /path/to/destination Fortunately we have lots of extra checks and I caught this before ever executing it, but it was scary to even just see that. -- Alex
_______________________________________________ Chicken-hackers mailing list [email protected] https://lists.nongnu.org/mailman/listinfo/chicken-hackers
