hi,
instead of having to do:
reverse(reverse(foo/"/")[1..]);
i'd very much prefer:
(foo/"/")[..-2];
are there any reasons that speak against supporting this other than me not being able to offer a patch?
greetings, martin.
Yes, it breaks code. Perhaps [x:y] could be used, where x and y may be negative to count from the end of the array. Three last characters in a: a[-3:]
/ Martin Nilsson (har bott i google)
Previous text:
2003-03-05 22:53: Subject: negative indices in array ranges
hi,
instead of having to do:
reverse(reverse(foo/"/")[1..]);
i'd very much prefer:
(foo/"/")[..-2];
are there any reasons that speak against supporting this other than me not being able to offer a patch?
greetings, martin.
/ Brevbäraren
I think a function would be better than a special syntax, if we can find a good name for it. Shouldn't be hard to find one since almost every language got one or more functions for that purpose. E.g. "substr" in Perl, "mid" in BASIC. More?
/ Martin Stjernholm, Roxen IS
Previous text:
2003-03-05 22:56: Subject: negative indices in array ranges
Yes, it breaks code. Perhaps [x:y] could be used, where x and y may be negative to count from the end of the array. Three last characters in a: a[-3:]
/ Martin Nilsson (har bott i google)
So what? It's only the names that we want to borrow anyway. Granted, "substr" is not a good name for a function that can handle arrays too, but those were only some examples I could take from the top of my head.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-03-05 23:25: Subject: Re: negative indices in array ranges
but those are for strings, this is about arrays.
greetings, martin.
/ Brevbäraren
Strings and arrays would be the same in this respect. Perhaps something like
prefix(string|array x, 17) // first 17 elements prefix(string|array x, -17) // all but the last 17 elements
suffix(string|array x, -17) // last 17 suffix(string|array x, 17) // all but the first 17
would be nice and clean? The number indicates a *position* in the string, where 0..length counts from the start of the string, and -length..-1 counts from the end. prefix returns the part of the string before this position, suffix the part efter the position.
Or one could reverse the role of the sign in the suffix function, if that's more intuitive, although at the moment I think
s == prefix(s, k) + suffix(s, k)
is nicer than
s == prefix(s, k) + suffix(s, -k)
Anyway, one should think carefully about off-by-one issues when designing this, from the last time we discussed it I think it was clear at least to me that the interpretation of negative numbers in ordinary indexing was sligthly wrong (sorry, I don't quite remeber the details), so one may have to choose between doing the right thing, or staying compatible with other indexing operations.
/ Niels Möller ()
Previous text:
2003-03-05 23:25: Subject: Re: negative indices in array ranges
but those are for strings, this is about arrays.
greetings, martin.
/ Brevbäraren
there would also be room for infix...
but i just thought about that this actually looks like another kind of split operation, instead of spliting on certain elements this will split on positions.
prefix would be split(foo, 17)[0]; postfix would be split(foo, 17)[1];
but you could split on any number of positions: [pre, in1, in2, post] = split(foo, 5, 7, 17);
greetings, martin.
The off-by-one issue is perhaps that prefix() wouldn't include the given position while suffix() would. That's a bit at odds with the range operator which takes an inclusive range in both directions. I.e.
suffix(x, 17) == x[17..]
but
prefix(x, 17) == x[..16]
/ Martin Stjernholm, Roxen IS
Previous text:
2003-03-05 23:35: Subject: Re: negative indices in array ranges
Strings and arrays would be the same in this respect. Perhaps something like
prefix(string|array x, 17) // first 17 elements prefix(string|array x, -17) // all but the last 17 elements
suffix(string|array x, -17) // last 17 suffix(string|array x, 17) // all but the first 17
would be nice and clean? The number indicates a *position* in the string, where 0..length counts from the start of the string, and -length..-1 counts from the end. prefix returns the part of the string before this position, suffix the part efter the position.
Or one could reverse the role of the sign in the suffix function, if that's more intuitive, although at the moment I think
s == prefix(s, k) + suffix(s, k)
is nicer than
s == prefix(s, k) + suffix(s, -k)
Anyway, one should think carefully about off-by-one issues when designing this, from the last time we discussed it I think it was clear at least to me that the interpretation of negative numbers in ordinary indexing was sligthly wrong (sorry, I don't quite remeber the details), so one may have to choose between doing the right thing, or staying compatible with other indexing operations.
/ Niels Möller ()
You're right. Thanks for reminding me!
prefix(s, 0) returns an empty string prefix, "". To get the same effekt with .., one have to use s[..-1]. And *that's* the reason why it's hard to support s[..-k] for counting from the end of the string.
/ Niels Möller ()
Previous text:
2003-03-06 00:59: Subject: Re: negative indices in array ranges
The off-by-one issue is perhaps that prefix() wouldn't include the given position while suffix() would. That's a bit at odds with the range operator which takes an inclusive range in both directions. I.e.
suffix(x, 17) == x[17..]
but
prefix(x, 17) == x[..16]
/ Martin Stjernholm, Roxen IS
I was thinking less generic, like "endcut(int n)". Other generic names include "crop" and "copy".
/ Mirar
Previous text:
2003-03-05 23:19: Subject: negative indices in array ranges
I think a function would be better than a special syntax, if we can find a good name for it. Shouldn't be hard to find one since almost every language got one or more functions for that purpose. E.g. "substr" in Perl, "mid" in BASIC. More?
/ Martin Stjernholm, Roxen IS
The problem with a function or a set of functions is to dream up a good syntax the allows for ranges with either side open as well as both sides closed, and to handle that efficient. But OTOH we can let the tree optimizer reduce it all to a[b..c] expressions.
/ Martin Nilsson (har bott i google)
Previous text:
2003-03-05 23:19: Subject: negative indices in array ranges
I think a function would be better than a special syntax, if we can find a good name for it. Shouldn't be hard to find one since almost every language got one or more functions for that purpose. E.g. "substr" in Perl, "mid" in BASIC. More?
/ Martin Stjernholm, Roxen IS
Why would it have to handle open ranges? 0 is the first element, -1 the last.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-03-06 00:34: Subject: negative indices in array ranges
The problem with a function or a set of functions is to dream up a good syntax the allows for ranges with either side open as well as both sides closed, and to handle that efficient. But OTOH we can let the tree optimizer reduce it all to a[b..c] expressions.
/ Martin Nilsson (har bott i google)
True. Then I think this is the best suggestion:
string|array crop(string|array x, int from, int to) { if(from<0) from += sizeof(x); if(to<0) to += sizeof(x); return x[from..to]; }
but preferably implemented in C so that we get the type
function((0=string|array),int,int:0)
and the optimization rule crop(a,b,c) -> a[b..c] if b and c >= 0.
/ Martin Nilsson (har bott i google)
Previous text:
2003-03-06 00:46: Subject: negative indices in array ranges
Why would it have to handle open ranges? 0 is the first element, -1 the last.
/ Martin Stjernholm, Roxen IS
Fairly nice, but "crop" sounds destructive to me. What about "range"?
and the optimization rule crop(a,b,c) -> a[b..c] if b and c >= 0.
Seems a bit pointless since a C implementation would be practically just as fast anyway.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-03-06 00:54: Subject: negative indices in array ranges
True. Then I think this is the best suggestion:
string|array crop(string|array x, int from, int to) { if(from<0) from += sizeof(x); if(to<0) to += sizeof(x); return x[from..to]; }
but preferably implemented in C so that we get the type
function((0=string|array),int,int:0)
and the optimization rule crop(a,b,c) -> a[b..c] if b and c >= 0.
/ Martin Nilsson (har bott i google)
Why can't it be destructive? :)
Ranges are the 1..2 part. Save that if we ever implement such a type?
/ Mirar
Previous text:
2003-03-06 01:10: Subject: negative indices in array ranges
Fairly nice, but "crop" sounds destructive to me. What about "range"?
and the optimization rule crop(a,b,c) -> a[b..c] if b and c >= 0.
Seems a bit pointless since a C implementation would be practically just as fast anyway.
/ Martin Stjernholm, Roxen IS
Because it isn't always possible to change strings and arrays destructively, of course. It isn't relevant if it sometimes is destructive as an optimization since that never has any observable effect (or else something is broken).
Ranges are the 1..2 part. Save that if we ever implement such a type?
What do you mean? "range" shouldn't be used as a function name since we might want to make it a type? What would such a type do?
/ Martin Stjernholm, Roxen IS
Previous text:
2003-03-06 07:35: Subject: negative indices in array ranges
Why can't it be destructive? :)
Ranges are the 1..2 part. Save that if we ever implement such a type?
/ Mirar
What do you mean? "range" shouldn't be used as a function name since we might want to make it a type? What would such a type do?
Contain for instance "1 to and inclusive 2". I think it's been discussed previously, but it might have been some years ago. Ie, {x;a<=x<=b}, {x;a<=x<b}, {x;a<x<=b} or {x;a<x<b}.
I don't say we should save it in case we implement the type, but I feel that a "range" function should work on or with such ranges, not arrays or other datatypes.
/ Mirar
Previous text:
2003-03-06 12:29: Subject: negative indices in array ranges
Because it isn't always possible to change strings and arrays destructively, of course. It isn't relevant if it sometimes is destructive as an optimization since that never has any observable effect (or else something is broken).
Ranges are the 1..2 part. Save that if we ever implement such a type?
What do you mean? "range" shouldn't be used as a function name since we might want to make it a type? What would such a type do?
/ Martin Stjernholm, Roxen IS
IMO that hypothetical future function doesn't weigh heavier than the one being discussed here. This one will be used quite often and "range" is a fitting name for it.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-03-06 12:37: Subject: negative indices in array ranges
What do you mean? "range" shouldn't be used as a function name since we might want to make it a type? What would such a type do?
Contain for instance "1 to and inclusive 2". I think it's been discussed previously, but it might have been some years ago. Ie, {x;a<=x<=b}, {x;a<=x<b}, {x;a<x<=b} or {x;a<x<b}.
I don't say we should save it in case we implement the type, but I feel that a "range" function should work on or with such ranges, not arrays or other datatypes.
/ Mirar
I'm still not talking about saving it for future use, I explained my reasons why I don't think "range" is a fitting name.
To put it in clear text: I don't think "range" fits what the function does.
In my opinion, reusing copy_value sounds like a much better idea. Changing it's name to "copy" sounds like a great idea.
Did anyone do any with the old suggestion of making the number of levels copied by copy_value a parameter?
/ Mirar
Previous text:
2003-03-06 12:48: Subject: negative indices in array ranges
IMO that hypothetical future function doesn't weigh heavier than the one being discussed here. This one will be used quite often and "range" is a fitting name for it.
/ Martin Stjernholm, Roxen IS
Ok, then we simply have different opinions on that one.
I otoh think it's a bit ugly to graft filtering functionality into copy_value; it seems arbitrary that such a generic function should handle ranges which isn't even applicable for many of the types it supports. It could just as well take an array of specific indices to copy, or a parameter to match prefixes on strings to copy, or something like that.
Adding a level parameter to copy_value sounds like a really good suggestion, though.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-03-06 12:54: Subject: negative indices in array ranges
I'm still not talking about saving it for future use, I explained my reasons why I don't think "range" is a fitting name.
To put it in clear text: I don't think "range" fits what the function does.
In my opinion, reusing copy_value sounds like a much better idea. Changing it's name to "copy" sounds like a great idea.
Did anyone do any with the old suggestion of making the number of levels copied by copy_value a parameter?
/ Mirar
I was just thinking of having an array (or multiset, anything with keys) to copy instead of ranges for the datatypes where that is more useful. But copying a selected range from a mapping or multiset isn't that far-fetched, I think I've had use for it sometime...
Ie, mixed range_min,range_max; mapping in,out=([]); foreach (in;mixed key;mixed data) if (range_min<=key && range_max>=key) out[key]=data; return out;
or, in this case, level--; ... out[key]=level?copy_value(data,level):data;
or so. (Add suitable magic to handle recursive datatypes.)
/ Mirar
Previous text:
2003-03-06 13:03: Subject: negative indices in array ranges
Ok, then we simply have different opinions on that one.
I otoh think it's a bit ugly to graft filtering functionality into copy_value; it seems arbitrary that such a generic function should handle ranges which isn't even applicable for many of the types it supports. It could just as well take an array of specific indices to copy, or a parameter to match prefixes on strings to copy, or something like that.
Adding a level parameter to copy_value sounds like a really good suggestion, though.
/ Martin Stjernholm, Roxen IS
Shouldn't "crop" on a range return everything _but_ the range? After all, to crop means to _cut away_.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2003-03-06 00:54: Subject: negative indices in array ranges
True. Then I think this is the best suggestion:
string|array crop(string|array x, int from, int to) { if(from<0) from += sizeof(x); if(to<0) to += sizeof(x); return x[from..to]; }
but preferably implemented in C so that we get the type
function((0=string|array),int,int:0)
and the optimization rule crop(a,b,c) -> a[b..c] if b and c >= 0.
/ Martin Nilsson (har bott i google)
No, the normal crop operation removes everything but the range. See XV and Gimp for nice graphical illustrations.
/ Peter Bortas
Previous text:
2003-03-06 10:17: Subject: negative indices in array ranges
Shouldn't "crop" on a range return everything _but_ the range? After all, to crop means to _cut away_.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
I'm not quite sure what you mean by "the normal crop operation" and how that is relevant to Pike. "Crop the right edge" means remove (part of) the right edge but leave everything else intact. In the same way, I would interpret "Crop 10..20" as removing (part of) the range 10..20, but leaving everything else intact.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2003-03-06 10:28: Subject: negative indices in array ranges
No, the normal crop operation removes everything but the range. See XV and Gimp for nice graphical illustrations.
/ Peter Bortas
Hence "copy" might be a better use. But "crop" has been used that way (keeping the range) for quite some time in graphical applications like the Image module, for instance.
/ Mirar
Previous text:
2003-03-06 10:17: Subject: negative indices in array ranges
Shouldn't "crop" on a range return everything _but_ the range? After all, to crop means to _cut away_.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
For an operation that gives a new array, I think copy is a good name. Then you could call it without the range parameters to get a copy of the whole array as well.
Hm, maybe the range parameters should simply be added to copy_value?
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2003-03-06 10:31: Subject: negative indices in array ranges
Hence "copy" might be a better use. But "crop" has been used that way (keeping the range) for quite some time in graphical applications like the Image module, for instance.
/ Mirar
Also, copy_value could be renamed to copy.
/ Per Hedbor ()
Previous text:
2003-03-06 10:36: Subject: negative indices in array ranges
For an operation that gives a new array, I think copy is a good name. Then you could call it without the range parameters to get a copy of the whole array as well.
Hm, maybe the range parameters should simply be added to copy_value?
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
I can think of both "copy" and "crop", where the first gives a copy and the second works destructively. And a third function that removes a part of the datatype, but I can't recall any good names for that right now.
It would be logical if it worked on mappings and multisets as well, maybe using `<? (I assume that it will work on strings.)
/ Mirar
Previous text:
2003-03-06 10:36: Subject: negative indices in array ranges
For an operation that gives a new array, I think copy is a good name. Then you could call it without the range parameters to get a copy of the whole array as well.
Hm, maybe the range parameters should simply be added to copy_value?
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Speaking of copy_value, I've often missed a copying function that isn't recursive. That's another one that doesn't exist only because noone found a good name for it. The best I can think of is "copy_level".
/ Martin Stjernholm, Roxen IS
Previous text:
2003-03-06 10:36: Subject: negative indices in array ranges
For an operation that gives a new array, I think copy is a good name. Then you could call it without the range parameters to get a copy of the whole array as well.
Hm, maybe the range parameters should simply be added to copy_value?
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Can I suggest perhaps
mixed copy(mixed what, void|int(0..) levels, void|mixed range_min, void|mixed range_max)
?
/ Mirar
Previous text:
2003-03-06 12:52: Subject: negative indices in array ranges
Speaking of copy_value, I've often missed a copying function that isn't recursive. That's another one that doesn't exist only because noone found a good name for it. The best I can think of is "copy_level".
/ Martin Stjernholm, Roxen IS
Woha! One character gain!
copy(x,-1,0,-2) x[..sizeof(x)-2]
/ Martin Nilsson (har bott i google)
Previous text:
2003-03-06 12:56: Subject: negative indices in array ranges
Can I suggest perhaps
mixed copy(mixed what, void|int(0..) levels, void|mixed range_min, void|mixed range_max)
?
/ Mirar
I'm more conserned that it is harder to read. I'm not a bit fan of commonly used functions that are fed a large number of hard to place integers.
/ Peter Bortas
Previous text:
2003-03-06 14:09: Subject: negative indices in array ranges
Woha! One character gain!
copy(x,-1,0,-2) x[..sizeof(x)-2]
/ Martin Nilsson (har bott i google)
I prefere range(variable,start,end) for that reason, and have an optional argument to 'copy' that means the number of levels that will be copied.
/ Per Hedbor ()
Previous text:
2003-03-06 14:17: Subject: negative indices in array ranges
I'm more conserned that it is harder to read. I'm not a bit fan of commonly used functions that are fed a large number of hard to place integers.
/ Peter Bortas
Agreed.
/ Martin Nilsson (har bott i google)
Previous text:
2003-03-06 14:21: Subject: negative indices in array ranges
I prefere range(variable,start,end) for that reason, and have an optional argument to 'copy' that means the number of levels that will be copied.
/ Per Hedbor ()
Yes. filter_range(x,a,b) might me much more logical. filter(x,c) already exist (where c is a function, multiset or mapping).
/ Mirar
Previous text:
2003-03-06 14:17: Subject: negative indices in array ranges
I'm more conserned that it is harder to read. I'm not a bit fan of commonly used functions that are fed a large number of hard to place integers.
/ Peter Bortas
In all programs I use crop means keep selection.
/ Martin Nilsson (har bott i google)
Previous text:
2003-03-06 10:17: Subject: negative indices in array ranges
Shouldn't "crop" on a range return everything _but_ the range? After all, to crop means to _cut away_.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
That is how it's generally used in graphics programs. However that doesn't change the fact that crop DOES mean "to cut off". Granted, most computer users would understand the meaning of 'crop' that is "mark an area and crop away anything not marked".
/ David Hedbor
Previous text:
2003-03-06 14:06: Subject: negative indices in array ranges
In all programs I use crop means keep selection.
/ Martin Nilsson (har bott i google)
TI-BASIC called it "SEG$", for "segment".
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2003-03-05 23:19: Subject: negative indices in array ranges
I think a function would be better than a special syntax, if we can find a good name for it. Shouldn't be hard to find one since almost every language got one or more functions for that purpose. E.g. "substr" in Perl, "mid" in BASIC. More?
/ Martin Stjernholm, Roxen IS
which code would it break?
any range ending in a negative value create an empty array, negative values at the beginning are interpreted as 0.
i can't see a situation where such an interpretation would make sense.
greetings, martin.
Afaik it's primarily to make it possible to remove an element i with arr[..i-1]+arr[i+1..] without having to bother with special cases for the first (and last) elements.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-03-05 23:30: Subject: Re: negative indices in array ranges
which code would it break?
any range ending in a negative value create an empty array, negative values at the beginning are interpreted as 0.
i can't see a situation where such an interpretation would make sense.
greetings, martin.
/ Brevbäraren
It is also used in many places to avoid having to do boundry checks of the used variables.
/ Martin Nilsson (har bott i google)
Previous text:
2003-03-05 23:34: Subject: Re: negative indices in array ranges
Afaik it's primarily to make it possible to remove an element i with arr[..i-1]+arr[i+1..] without having to bother with special cases for the first (and last) elements.
/ Martin Stjernholm, Roxen IS
IIRC, it's discussed about once every year. I'm sure there are reasons enough not to allow it. I can't see any reasons not to add a function to do that operation on arrays, though, it's a common enough problem.
combine_path(foo,"..")/"/"
might solve *your* problem, though. :)
/ Mirar
Previous text:
2003-03-05 22:53: Subject: negative indices in array ranges
hi,
instead of having to do:
reverse(reverse(foo/"/")[1..]);
i'd very much prefer:
(foo/"/")[..-2];
are there any reasons that speak against supporting this other than me not being able to offer a patch?
greetings, martin.
/ Brevbäraren
On Wed, Mar 05, 2003 at 11:00:01PM +0100, Mirar @ Pike developers forum wrote:
IIRC, it's discussed about once every year.
forgive me, without an mbox archive of this forum searching is hard for me.
combine_path(foo,"..")/"/" might solve *your* problem, though. :)
indeed, thanks for the reminder, in fact combine_path(foo,".."); is what i need.
greetings, martin.
forgive me, without an mbox archive of this forum searching is hard for me.
No worries. I'm sure someone will explain the reasons again. I always forget them. :)
in fact combine_path(foo,".."); is what i need.
That's what I thought :)
/ Mirar
Previous text:
2003-03-05 23:06: Subject: Re: negative indices in array ranges
On Wed, Mar 05, 2003 at 11:00:01PM +0100, Mirar @ Pike developers forum wrote:
IIRC, it's discussed about once every year.
forgive me, without an mbox archive of this forum searching is hard for me.
combine_path(foo,"..")/"/" might solve *your* problem, though. :)
indeed, thanks for the reminder, in fact combine_path(foo,".."); is what i need.
greetings, martin.
/ Brevbäraren
IIRC, it's discussed about once every year.
forgive me, without an mbox archive of this forum searching is hard for me.
Well, it's just the discussion that ends up being had once a year; the environment and participants tends to shift over the years. :-)
/ Johan Sundström (folkskådare)
Previous text:
2003-03-05 23:06: Subject: Re: negative indices in array ranges
On Wed, Mar 05, 2003 at 11:00:01PM +0100, Mirar @ Pike developers forum wrote:
IIRC, it's discussed about once every year.
forgive me, without an mbox archive of this forum searching is hard for me.
combine_path(foo,"..")/"/" might solve *your* problem, though. :)
indeed, thanks for the reminder, in fact combine_path(foo,".."); is what i need.
greetings, martin.
/ Brevbäraren
pike-devel@lists.lysator.liu.se