1. Your thoughts on two item id methods?
Hello there,
Im looking at something that needs to tell the difference between two
different classes of elements in a loop. The two methods presented
here both work, but they work somewhat different. I'd like to hear
any thoughts anyone has on these two methods, as to which one if any
seems better and why...
METHOD #1
Atoms are of one class, while sequences are of the other class.
Here, we know items of class 1 are atoms, while class 2 items are sequences.
This way we can tell the difference between class 1 items and class 2 items...
itemlist={ 1,2,3,{8,9},4,{6,7} } --atoms and sequences
for k=1 to length(itemlist) do
item=itemlist[k]--atom or seq
if atom(item) then
DoClass1Stuff(item)
else
DoClass2Stuff(item)
end if
end for
METHOD #2
Atoms will be in a sequence where the first element is zero, while
the other class will always have a '1' as first element with the real
data in elements 2 and above. Thus, class 1 elements start with a zero
and class 2 elements start with a 1...
itemlist={ {0,1},{0,2},{0,3},{1,8,9},{0,4},{1,6,7} } --all sequences
for k=1 to length(itemlist) do
item=itemlist[k]--always a sequence
if item[1] then
DoClass2Stuff(item)
else
DoClass1Stuff(item)
end if
end for
I'm not that worried about the increase in memory space taken by
the second method.
In each itemlist there will be approx the same number of class 1 items
as there are class 2 items.
Thanks for your time and thoughts...
Take care,
Al
And, good luck with your Euphoria programming!
My bumper sticker: "I brake for LED's"
2. Re: Your thoughts on two item id methods?
Al Getz wrote:
>Im looking at something that needs to tell the difference between two
>different classes of elements in a loop. The two methods presented
>here both work, but they work somewhat different. I'd like to hear
>any thoughts anyone has on these two methods, as to which one if any
>seems better and why...
>
>METHOD #1
>
>Atoms are of one class, while sequences are of the other class.
>Here, we know items of class 1 are atoms, while class 2 items are sequences.
>This way we can tell the difference between class 1 items and class 2 items...
>
>itemlist={ 1,2,3,{8,9},4,{6,7} } --atoms and sequences
>for k=1 to length(itemlist) do
> item=itemlist[k]--atom or seq
> if atom(item) then
> DoClass1Stuff(item)
> else
> DoClass2Stuff(item)
> end if
>end for
>
>
>METHOD #2
>
>Atoms will be in a sequence where the first element is zero, while
>the other class will always have a '1' as first element with the real
>data in elements 2 and above. Thus, class 1 elements start with a zero
>and class 2 elements start with a 1...
>
>itemlist={ {0,1},{0,2},{0,3},{1,8,9},{0,4},{1,6,7} } --all sequences
>
>for k=1 to length(itemlist) do
> item=itemlist[k]--always a sequence
> if item[1] then
> DoClass2Stuff(item)
> else
> DoClass1Stuff(item)
> end if
>end for
>
>I'm not that worried about the increase in memory space taken by
>the second method.
>In each itemlist there will be approx the same number of class 1 items
>as there are class 2 items.
>
Method 1 uses less memory, which you don't care about, but it will also
run faster. The atom() type check is extremely fast, probably in the
ballpark of an "if variable then" construct, though likely just a bit
slower. But it is sure to be faster than "if variable[subscript] then".
Also, "DoClassStuff(item)" may need a slice in method 2 than method 1
won't require.
However, this is only workable if you will only need to distinguish atom
and sequence elements--it does not generalize. Say at some point you
need to quickly distinguish ordinary sequences from character
strings--now you could prefix the strings with a 2, but method 1 would
require you to check the whole element to see if it is a string.
-- Mike Nelson
3. Re: Your thoughts on two item id methods?
Al Getz wrote:
>
> Hello there,
>
>
> Im looking at something that needs to tell the difference between two
> different classes of elements in a loop. The two methods presented
> here both work, but they work somewhat different. I'd like to hear
> any thoughts anyone has on these two methods, as to which one if any
> seems better and why...
>
> METHOD #1
>
> Atoms are of one class, while sequences are of the other class.
> Here, we know items of class 1 are atoms, while class 2 items are sequences.
> This way we can tell the difference between class 1 items and class 2 items...
>
> itemlist={ 1,2,3,{8,9},4,{6,7} } --atoms and sequences
> for k=1 to length(itemlist) do
> item=itemlist[k]--atom or seq
> if atom(item) then
> DoClass1Stuff(item)
> else
> DoClass2Stuff(item)
> end if
> end for
>
>
> METHOD #2
>
> Atoms will be in a sequence where the first element is zero, while
> the other class will always have a '1' as first element with the real
> data in elements 2 and above. Thus, class 1 elements start with a zero
> and class 2 elements start with a 1...
>
> itemlist={ {0,1},{0,2},{0,3},{1,8,9},{0,4},{1,6,7} } --all sequences
>
> for k=1 to length(itemlist) do
> item=itemlist[k]--always a sequence
> if item[1] then
> DoClass2Stuff(item)
> else
> DoClass1Stuff(item)
> end if
> end for
>
> I'm not that worried about the increase in memory space taken by
> the second method.
> In each itemlist there will be approx the same number of class 1 items
> as there are class 2 items.
>
Al:
Why can't you doing this:
itemlist={ 1,2,3,{8,9},4,{6,7} } --atoms and sequences
for k=1 to length(itemlist) do
if atom(itemlist[k]) then
DoClass1Stuff(itemlist[k])
else
DoClass2Stuff(itemlist[k])
end if
end for
Bernie
My files in archive:
w32engin.ew mixedlib.e eu_engin.e win32eru.exw
Can be downloaded here:
http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan
4. Re: Your thoughts on two item id methods?
Bernie
My files in archive:
w32engin.ew mixedlib.e eu_engin.e win32eru.exw
Can be downloaded here:
http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan
Al:
Or this:
itemlist={ 1,2,3,{8,9},4,{6,7} } --atoms and sequences
for k=1 to length(itemlist) do
if atom(itemlist[k]) then DoClass1Stuff(itemlist[k]) end if
DoClass2Stuff(itemlist[k])
end for
5. Re: Your thoughts on two item id methods?
Al:
IGNORE LAST POST
Bernie
My files in archive:
w32engin.ew mixedlib.e eu_engin.e win32eru.exw
Can be downloaded here:
http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan
6. Re: Your thoughts on two item id methods?
Michael Nelson wrote:
>
>
> Al Getz wrote:
>
> >Im looking at something that needs to tell the difference between two
> >different classes of elements in a loop. The two methods presented
> >here both work, but they work somewhat different. I'd like to hear
> >any thoughts anyone has on these two methods, as to which one if any
> >seems better and why...
> >
> >METHOD #1
> >
> >Atoms are of one class, while sequences are of the other class.
> >Here, we know items of class 1 are atoms, while class 2 items are sequences.
> >This way we can tell the difference between class 1 items and class 2
> >items...
> >
> >itemlist={ 1,2,3,{8,9},4,{6,7} } --atoms and sequences
> >for k=1 to length(itemlist) do
> > item=itemlist[k]--atom or seq
> > if atom(item) then
> > DoClass1Stuff(item)
> > else
> > DoClass2Stuff(item)
> > end if
> >end for
> >
> >
> >METHOD #2
> >
> >Atoms will be in a sequence where the first element is zero, while
> >the other class will always have a '1' as first element with the real
> >data in elements 2 and above. Thus, class 1 elements start with a zero
> >and class 2 elements start with a 1...
> >
> >itemlist={ {0,1},{0,2},{0,3},{1,8,9},{0,4},{1,6,7} } --all sequences
> >
> >for k=1 to length(itemlist) do
> > item=itemlist[k]--always a sequence
> > if item[1] then
> > DoClass2Stuff(item)
> > else
> > DoClass1Stuff(item)
> > end if
> >end for
> >
> >I'm not that worried about the increase in memory space taken by
> >the second method.
> >In each itemlist there will be approx the same number of class 1 items
> >as there are class 2 items.
> >
>
> Method 1 uses less memory, which you don't care about, but it will also
> run faster. The atom() type check is extremely fast, probably in the
> ballpark of an "if variable then" construct, though likely just a bit
> slower. But it is sure to be faster than "if variable[subscript] then".
> Also, "DoClassStuff(item)" may need a slice in method 2 than method 1
> won't require.
>
> However, this is only workable if you will only need to distinguish atom
> and sequence elements--it does not generalize. Say at some point you
> need to quickly distinguish ordinary sequences from character
> strings--now you could prefix the strings with a 2, but method 1 would
> require you to check the whole element to see if it is a string.
>
> -- Mike Nelson
>
>
Hi Mike,
I think i see what you're saying here...that method 2 is more
easily extensible than method 1, although method 1 is a little faster.
I guess this requires some thought: go with something a little faster
or with something that will be easier to update in the future perhaps.
Once the code is written there will be quite a few routines that depend
on the data structure so it's not an easy decision, but going with the
more extensible is sounding good right now just in case the program needs
that kind of update. With method 1 atoms are limited in what they
can be too, which might not be that good of an idea.
Take care,
Al
Take care,
Al
And, good luck with your Euphoria programming!
My bumper sticker: "I brake for LED's"
7. Re: Your thoughts on two item id methods?
Bernie Ryan wrote:
>
> Al Getz wrote:
> >
> > Hello there,
> >
> >
> > Im looking at something that needs to tell the difference between two
> > different classes of elements in a loop. The two methods presented
> > here both work, but they work somewhat different. I'd like to hear
> > any thoughts anyone has on these two methods, as to which one if any
> > seems better and why...
> >
> > METHOD #1
> >
> > Atoms are of one class, while sequences are of the other class.
> > Here, we know items of class 1 are atoms, while class 2 items are sequences.
> > This way we can tell the difference between class 1 items and class 2
> > items...
> >
> > itemlist={ 1,2,3,{8,9},4,{6,7} } --atoms and sequences
> > for k=1 to length(itemlist) do
> > item=itemlist[k]--atom or seq
> > if atom(item) then
> > DoClass1Stuff(item)
> > else
> > DoClass2Stuff(item)
> > end if
> > end for
> >
> >
> > METHOD #2
> >
> > Atoms will be in a sequence where the first element is zero, while
> > the other class will always have a '1' as first element with the real
> > data in elements 2 and above. Thus, class 1 elements start with a zero
> > and class 2 elements start with a 1...
> >
> > itemlist={ {0,1},{0,2},{0,3},{1,8,9},{0,4},{1,6,7} } --all sequences
> >
> > for k=1 to length(itemlist) do
> > item=itemlist[k]--always a sequence
> > if item[1] then
> > DoClass2Stuff(item)
> > else
> > DoClass1Stuff(item)
> > end if
> > end for
> >
> > I'm not that worried about the increase in memory space taken by
> > the second method.
> > In each itemlist there will be approx the same number of class 1 items
> > as there are class 2 items.
> >
>
> Al:
> Why can't you doing this:
>
> itemlist={ 1,2,3,{8,9},4,{6,7} } --atoms and sequences
>
> for k=1 to length(itemlist) do
> if atom(itemlist[k]) then
> DoClass1Stuff(itemlist[k])
> else
> DoClass2Stuff(itemlist[k])
> end if
> end for
>
>
> Bernie
>
> My files in archive:
> w32engin.ew mixedlib.e eu_engin.e win32eru.exw
>
> Can be downloaded here:
> <a
> href="http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan">http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan</a>
>
Hi Bernie,
Yes, that might help too, but the main decision here is to go with
the data structure of Method 1 or of Method 2. Once the routines are
written they will depend on the data structure so it's a tough decision.
It would be hard to change later on.
Take care,
Al
And, good luck with your Euphoria programming!
My bumper sticker: "I brake for LED's"
8. Re: Your thoughts on two item id methods?
Al Getz wrote:
>
> Hello there,
>
>
> Im looking at something that needs to tell the difference between two
> different classes of elements in a loop. The two methods presented
> here both work, but they work somewhat different. I'd like to hear
> any thoughts anyone has on these two methods, as to which one if any
> seems better and why...
>
> METHOD #1
>
> Atoms are of one class, while sequences are of the other class.
> Here, we know items of class 1 are atoms, while class 2 items are sequences.
> This way we can tell the difference between class 1 items and class 2 items...
>
> itemlist={ 1,2,3,{8,9},4,{6,7} } --atoms and sequences
> for k=1 to length(itemlist) do
> item=itemlist[k]--atom or seq
> if atom(item) then
> DoClass1Stuff(item)
> else
> DoClass2Stuff(item)
> end if
> end for
>
>
> METHOD #2
>
> Atoms will be in a sequence where the first element is zero, while
> the other class will always have a '1' as first element with the real
> data in elements 2 and above. Thus, class 1 elements start with a zero
> and class 2 elements start with a 1...
>
> itemlist={ {0,1},{0,2},{0,3},{1,8,9},{0,4},{1,6,7} } --all sequences
>
> for k=1 to length(itemlist) do
> item=itemlist[k]--always a sequence
> if item[1] then
> DoClass2Stuff(item)
> else
> DoClass1Stuff(item)
> end if
> end for
>
> I'm not that worried about the increase in memory space taken by
> the second method.
> In each itemlist there will be approx the same number of class 1 items
> as there are class 2 items.
>
> Thanks for your time and thoughts...
>
>
> Take care,
> Al
>
> And, good luck with your Euphoria programming!
>
> My bumper sticker: "I brake for LED's"
>
I would use the second method, because it is more extensible as has been pointed
out.
One thing that I noticed while thinking about strings and structures, is that
for any sequence except the empty sequence, you can actually acess two known
members: sequence[1] and sequence[$]. I was thinking to add type-data to
sequence[$] in sequences representing strings or structures. That way normal
elements can be accessed with sequence[1..$] in a more natural way.
Or embed a routine id as your marker value and for each loop iteration do
dc1 = routine_id("DoClass1Stuff")
dc2 = routine_id("DoClass2Stuff")
itemlist={ {dc1,1},{dc1,2},{dc1,3},{dc2,8,9},{dc1,4},{dc2,6,7} } --all sequences
for k=1 to length(itemlist) do
item=itemlist[k]--always a sequence
call_proc(item[1], {item})
end for
j.
9. Re: Your thoughts on two item id methods?
Jason Gade wrote:
>
> Al Getz wrote:
> >
> > Hello there,
> >
> >
> > Im looking at something that needs to tell the difference between two
> > different classes of elements in a loop. The two methods presented
> > here both work, but they work somewhat different. I'd like to hear
> > any thoughts anyone has on these two methods, as to which one if any
> > seems better and why...
> >
> > METHOD #1
> >
> > Atoms are of one class, while sequences are of the other class.
> > Here, we know items of class 1 are atoms, while class 2 items are sequences.
> > This way we can tell the difference between class 1 items and class 2
> > items...
> >
> > itemlist={ 1,2,3,{8,9},4,{6,7} } --atoms and sequences
> > for k=1 to length(itemlist) do
> > item=itemlist[k]--atom or seq
> > if atom(item) then
> > DoClass1Stuff(item)
> > else
> > DoClass2Stuff(item)
> > end if
> > end for
> >
> >
> > METHOD #2
> >
> > Atoms will be in a sequence where the first element is zero, while
> > the other class will always have a '1' as first element with the real
> > data in elements 2 and above. Thus, class 1 elements start with a zero
> > and class 2 elements start with a 1...
> >
> > itemlist={ {0,1},{0,2},{0,3},{1,8,9},{0,4},{1,6,7} } --all sequences
> >
> > for k=1 to length(itemlist) do
> > item=itemlist[k]--always a sequence
> > if item[1] then
> > DoClass2Stuff(item)
> > else
> > DoClass1Stuff(item)
> > end if
> > end for
> >
> > I'm not that worried about the increase in memory space taken by
> > the second method.
> > In each itemlist there will be approx the same number of class 1 items
> > as there are class 2 items.
> >
> > Thanks for your time and thoughts...
> >
> >
> > Take care,
> > Al
> >
> > And, good luck with your Euphoria programming!
> >
> > My bumper sticker: "I brake for LED's"
> >
>
> I would use the second method, because it is more extensible as has been
> pointed
> out.
>
> One thing that I noticed while thinking about strings and structures, is that
> for any sequence except the empty sequence, you can actually acess two known
> members: sequence[1] and sequence[$]. I was thinking to add type-data to
> sequence[$]
> in sequences representing strings or structures. That way normal elements can
> be accessed with sequence[1..$] in a more natural way.
>
> Or embed a routine id as your marker value and for each loop iteration do
>
> }}}
<eucode>
> dc1 = routine_id("DoClass1Stuff")
> dc2 = routine_id("DoClass2Stuff")
> itemlist={ {dc1,1},{dc1,2},{dc1,3},{dc2,8,9},{dc1,4},{dc2,6,7} } --all
> sequences
>
> for k=1 to length(itemlist) do
> item=itemlist[k]--always a sequence
> call_proc(item[1], {item})
> end for
> </eucode>
{{{
>
>
> j.
>
Hi Jason,
Sounds good...I'll probably end up doing something like that too.
Im doing some more testing right now then i'll have to start
converting to the new form.
It's interesting that these kind of situations come up now and then,
where two methods are almost the same with little difference, and
you have to decide which to use. One is better for one reason and
the other is better for another reason. Just like chess, where a
master i used to play against used to always say (after we evaluated
a position for a while), "You can't have everything", meaning after
a while you have to choose one evil over the other :)
Take care,
Al
And, good luck with your Euphoria programming!
My bumper sticker: "I brake for LED's"