1. subscripts
- Posted by Lewis Townsend <keroltarr at HOTMAIL.COM>
May 09, 2000
-
Last edited May 10, 2000
Hello Everyone, and Robert especially,
Someone a while back suggested that there should be a built
in "subscript" datatype. A variable declared as a subscript
could be used as a subscript to a sequence. I don't think I
saw the necessity of it at the time but I just stumbled upon
a situation in which this would be highly useful and make a
lot of sense. One thing that I would change about this new
feature is that a regular sequence could be used as a
subscript so it wouldn't require a special data type.
Currently Euphoria errors out if you try this I think.
Here is an example of what I'm talking about:
object a, b, c
a = {{1, "Hello", 99}, 45, "World", {{9},8}, 432}
b = {1, 2, 1}
c = a [b] -- this would work like x[1][2][1]
puts (1, c) -- prints "H" from "Hello"
Anyone have any comments? Rob? This seems like an invaluable
tool to me. I think it would also be consistent with the
existing syntax, it wouldn't break anything and would allow
more flexability. If anyone is interested in why (specifically)
I want this, I'll be glad to post a more real-life example.
later,
Lewis Townsend
keroltarr at hotmail.com
geocities.com/keroltarr
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
2. Re: subscripts
- Posted by Robert Craig <rds at ATTCANADA.NET>
May 09, 2000
-
Last edited May 10, 2000
Lewis Townsend writes:
> b = {1, 2, 1}
> c = a [b] -- this would work like x[1][2][1]
I think you meant a[1][2][1].
It seems logical that Euphoria should define
what it means to subscript a sequence with a sequence.
Another way to define it would be to allow code like:
a["Lewis"] = 99
a["Townsend"] = {1,2,3}
? a["Lewis"] + a["Townsend"]
I haven't been impressed enough with either way.
They are both fairly easy to implement, and won't
break existing code, but I suspect that neither would
be used that often or with that much advantage over
alternative ways of coding. I don't want to clutter
up the language with lots of features that are rarely
of much use. It would also allow some
subscript errors to evade immediate detection.
> If anyone is interested in why (specifically)
> I want this, I'll be glad to post a more real-life example.
Sure. Let's see it.
Regards,
Rob Craig
Rapid Deployment Software
http://www.RapidEuphoria.com
3. Re: subscripts
Hello Robert,
>Lewis Townsend writes:
> > b = {1, 2, 1}
> > c = a [b] -- this would work like x[1][2][1]
>
>I think you meant a[1][2][1].
Yes, I originally used variables like x,i, and a and
decided to change it but forgot to change everything.
>It seems logical that Euphoria should define
>what it means to subscript a sequence with a sequence.
>Another way to define it would be to allow code like:
>
> a["Lewis"] = 99
> a["Townsend"] = {1,2,3}
> ? a["Lewis"] + a["Townsend"]
I don't think I understand this syntax. Does it mean
that it looks for the string "Lewis" in a and replaces
it wit 99? This hardly seems logical but maybe you are
doing something else? In my proposed syntax:
a["Lewis"] = 99 -- would convert to:
a[['L']['e']['w']['i']['s']] = 99
which would generate an error because the innermost
set of subscripts isn't even subscripting anything!
This isn't at all what I was talking about.
>I haven't been impressed enough with either way.
>They are both fairly easy to implement, and won't
>break existing code, but I suspect that neither would
>be used that often or with that much advantage over
>alternative ways of coding. I don't want to clutter
>up the language with lots of features that are rarely
>of much use. It would also allow some
>subscript errors to evade immediate detection.
What about vertical slicing? I believe this would be used
quite often by lots of people. Would it be easy to implement?
Also, I guess that my main point is that I would like sequences
to be legal subscripts. Of cours it could still flag an error
if someone tried this:
x = {1,2}
a = {1,2,3,4}
? a [x] -- ERROR
-- obviously 'a' isn't a two deminsional sequence so it would
-- error out with a subcripting an atom message.
> > If anyone is interested in why (specifically)
> > I want this, I'll be glad to post a more real-life example.
>
>Sure. Let's see it.
Okay, I have some code I'm working on (and will post it too
if you like), but here is the jist of what I am doing:
I have a sequence that defines the structure of another sequence.
Let's call the first sequence 'human'. The second: 'Lewis'
human = {{"Name", {"First", "Last"}}, "Age"}
Lewis {{"Lewis", "Townsend"}, 21}
Can you see the relation ship between these?
I now have a function that is fed 'human' and a string
that coresponds to a string inside 'human'
x = GetSubscript (human, "Name")
-- This would return {1} becuase "Name" is the name of
-- the first subsequence of 'human'
x = GetSubscript (human, "Name.Last") --I do parsing first
-- This would return {1,2}
-- First it looks for "Name" and then it looks for "Last"
-- returning the appropriate subscripts
I would then like to use these sequences to subscript the
'Lewis' sequence:
x = GetSubscript (human, "Age") -- {2}
Lewis [x] += 1 -- Lewis [2] = 22
x = GetSubscript (human, "Name.Last") -- {1,2}
puts (1, Lewis [x]) -- prints "Townsend"
Obviously this would be helpful with OOP for Euphoria.
You could define classes as I have defined 'human' and
you could define objects similar to how I defined 'Lewis'.
I will post more specific code if you want. This would
allow having "Name" in different places for different
classes. Let me know what you think.
later,
Lewis Townsend
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
4. Re: subscripts
Lewis Townsend writes
>> a["Lewis"] = 99
>> a["Townsend"] = {1,2,3}
>> ? a["Lewis"] + a["Townsend"]
> I don't think I understand this syntax. Does it mean
> that it looks for the string "Lewis" in a and replaces
> it wit 99? This hardly seems logical but maybe you are
> doing something else?
> In my proposed syntax:
> a["Lewis"] = 99 -- would convert to:
> a[['L']['e']['w']['i']['s']] = 99
Sorry, I didn't really explain this well.
I meant that you could generalize the current notion
of what a sequence is, and turn it into a kind of
associative array, where any Euphoria object could
be associated with any Euphoria object.
The interpreter would maintain a list like:
sequence a:
subscript value
1 0
2 {0,0,{9},1}
3 {}
"Lewis" 99
"Townsend" {1, 2, 3}
When you read a subscript, the interpreter would
find that subscript in the first column and return the
associated value in the second column.
Similarly, you could overwrite a subscripted value with
a[1] = 10
or,
a["Townsend"] = {1,2,4}
I guess the values could also be generalized sequences
so you might see code like:
a["Rob"]["Craig"] = 9.9
i.e. a subscript could be any Euphoria object, but it
only does one level of subscripting. Perl "hashes"
are a primitive version of what I've described here.
As I said, I'm not really convinced that it would be
worth it to add all this stuff, either your proposal
or the one above.
Regards,
Rob Craig
Rapid Deployment Software
http://www.RapidEuphoria.com
5. Re: subscripts
> I meant that you could generalize the current notion
> of what a sequence is, and turn it into a kind of
> associative array, where any Euphoria object could
> be associated with any Euphoria object.
> The interpreter would maintain a list like:
>
> sequence a:
>
> subscript value
>
> 1 0
> 2 {0,0,{9},1}
> 3 {}
> "Lewis" 99
> "Townsend" {1, 2, 3}
>
> When you read a subscript, the interpreter would
> find that subscript in the first column and return the
> associated value in the second column.
> Similarly, you could overwrite a subscripted value with
> a[1] = 10
> or,
> a["Townsend"] = {1,2,4}
> I guess the values could also be generalized sequences
> so you might see code like:
> a["Rob"]["Craig"] = 9.9
> i.e. a subscript could be any Euphoria object, but it
> only does one level of subscripting. Perl "hashes"
> are a primitive version of what I've described here.
you mean like a structure? it would be very useful.
then you could know where value in sequence is
now you must remember numbers, and look at comments each time when you want
to acces sequence members
then you would have logical names for structure (=sequence) member
now:
s[1]="name"
s[2]="surname"
then:
s.name="name"
s.surname="surname"
like in C!
i am writing preprocessor currently (slow progressing cause of school shit)
which will support structures as first thing, then i will add some more
features
it will be my very own language!
6. Re: subscripts
The ultimate fetch ? :
function fetch(object a, sequence b)
for i=1 to length(b) do
a = a[b[i]]
end for
return a
end function
Sorry. jiri
7. Re: subscripts
Keeps Going and Going and Going.............................
Couldn't help it
8. Re: subscripts
Me again, Lewis,
Just in case you want the complementary store function, I have that
too:
function store(object a, sequence b, object c)
-- store c in a using index sequence b
integer len
len = length(b)
if len > 1 then
a[b[1]] = store(a[b[1]], b[2..len], c)
else
a[b[1]] = c
end if
return a
end function
Sorry, I could not do it non-recursive ;) jiri
9. Re: subscripts
Carl, I would not say pedantic - negligent is the word! ;)
The task was 'subscripting of sequences with *sequences*'.
Read the specs!
jiri
10. Re: subscripts
On Thu, 18 May 2000 01:47:10 +1200, jiri babor <jbabor at PARADISE.NET.NZ>
wrote:
>Carl, I would not say pedantic - negligent is the word! ;)
>The task was 'subscripting of sequences with *sequences*'.
>Read the specs!
Aah, sweet negligence. Especially when one is not the only guilty party:
Your while loop is semantically equivalent to my for loop. I've just added
a little error checking here and there to tidy the function up a little.
Otherwise they're exactly the same.
Neither function copes with the situation where 'b' is greater than the
depth/dimension of 'a' at the point it is accessed, though.
Moving swiftly along before things turn ugly; I think what we're trying to
implement here is some kind of associative array type...
global type aa(object x)
if atom(x) then return 0 end if
return remainder(length(x),2) = 0
end type
function aa_find(object index, aa array)
integer pos
pos = find(index, array)
pos *= remainder(pos,2) -- make sure it's an index
return pos
end function
global function aa_index(aa array, object index)
integer pos
pos = aa_find(index, array)
if pos then
return array[pos+1] -- return the associated data
end if
return array[0] -- deliberate error -- or we could handle it...
-- return {}
end function
global function aa_add(aa array, object index, object data)
integer pos
pos = aa_find(index, array)
if pos then
array[pos+1] = data
else
array = array & {index, data}
end if
return array
end function
global function aa_remove(aa array, object index)
integer pos
pos = aa_find(index, array)
if not pos then return array end if
array = array[1..pos-1] & array[pos+2..length(array)]
return array
end function
sequence animal
aa noise
noise = {}
noise = aa_add(noise, "cat", "miaow")
noise = aa_add(noise, "dog", "woof")
noise = aa_add(noise, "pig", "oink")
noise = aa_add(noise, "cow", "moo")
animal = "dog" -- change this
printf(1, "My %s says \"%s!\"\n", {animal, aa_index(noise, animal)})
-- End of code --
There's plenty of cunning things we could do with this. Even going to
multidimensional associativity. Not something I want to think about right
now
HTH,
Carl
--
Heeeere .sig .sig .sig .siggy...
11. Re: subscripts
Carl wrote:
>Your while loop is semantically equivalent to my for loop...
You do not read the specs, and you don't even read your mail. Some
eight or nine hours before you woke up I had posted my second note on the
subject. I quote in full:
>The ultimate fetch ? :
>
>function fetch(object a, sequence b)
> for i=1 to length(b) do
> a = a[b[i]]
> end for
> return a
>end function
>
>Sorry. jiri
At this point, I want to say I was not aware of Michael's function,
which is virtually identical. You see, I hate OO bloats in general,
and Michael's 'swiss army knife' style approach gives me creeps. But
by the sound of it, it may be worth reading after all, there might be
some real gems in it somewhere, hiding under improbable names...
Sorry, Michael :).
Carl continued meekly:
>I've just added a little error checking here and there to tidy the
>function up a little.
You call it 'error checking', I call it 'aiding and abetting'. When the
specs allow sequences only, and some one tries to smuggle an atom
through, s/he should be caught and punished!
Carl's associative arrays: very important topic. I would like to make
few comments too, but later, probably at lunch time... jiri
12. Re: subscripts
On Thu, 18 May 2000 11:38:26 +1200, Jiri Babor <J.Babor at GNS.CRI.NZ> wrote:
>Carl wrote:
>
>>Your while loop is semantically equivalent to my for loop...
>
>You do not read the specs, and you don't even read your mail. Some
>eight or nine hours before you woke up I had posted my second note on the
>subject...
Ah. Hmm. /me shuffles feet. /me speaks [lame] excuse:
I don't have time to read all of the mails sent to the ListServ interface,
just those that follow on via the "Next in topic" button.
I'd subscribe to the mailing list properly, but then I'd have several
hundred extra e-mails to read every month, and the boss doesn't like
unproductive employees...
Anyway, I digress. When I posted, the "Next" button was greyed out, and
therefore I assumed (obviously erroneously) that there were no more
messages in the thread. I back down and apologise to all those who think
I'm repeating what has already been said and done.
>You call it 'error checking', I call it 'aiding and abetting'. When the
>specs allow sequences only, and some one tries to smuggle an atom
>through, s/he should be caught and punished!
The actual spec must have been in a post I skipped for some reason, because
I don't remember seeing it. My fault.
I'm always on the lookout for ways to make things nice and generic, hence
the allowance of an atom instead of a single element sequence here and
there. I freely admit, though, that my code sometimes ends up being so
abstract it isn't useful anymore...
>Carl's associative arrays: very important topic. I would like to make
>few comments too, but later, probably at lunch time... jiri
At least my post wasn't all bad then.
The "aa" package could probably be better implemented though...
Carl
--
I saw that .sig a minute ago...