1. Structures (WAS Re: Memory)

Dear Euphorians,

Irv Mullins explained:

> >And while I'm at it, can anyone tell me specifically what the term
> >'structure' means ? Example; is it related to code or data? Please
> >convert your answear to my 2-digit IQ format smile
>
> Both. A structure is a way to "package" variables so they can be
> handled more conveniently.
> Example in unEuphoria code:
>   structure customer
>      sequence name, addr, city, state, zip, phone
>      integer age
>   end structure
>
> You could make assignments as follows:
>    customer.name = "John Smith"
>    customer.phone = "555-1212"
>    customer.age = 23
> and write to disk as puts(fn, customer)
> or assign the whole thing in one swell foop: thiscustomer = customer
> which does the same as:
>   thiscustomer.name = customer.name
>   thiscustomer.addr = customer.addr
>   thiscustomer.city = customer.city...and so on.

As Irv pointed out, structures are a way of conveniently handling
variables. I was surprised when I started learning Euphoria (I still
am ) that I couldn't find a structure (or struct, or TYPE, depending
on your background) statement in Euphoria. I would be in favour
of implementing this into a later Euphoria version, if possible. IMO, it
doesn't 'dirty up' the code by making it too complex, but rather
(like stated above) gives the programmer one more way of handling
variables, and a clean, clear way of doing it at that. Any opinions on this?


Chris Cox
cox.family at sk.sympatico.ca

new topic     » topic index » view message » categorize

2. Re: Structures (WAS Re: Memory)

With Euphoria's sequences, what need is there for structures, really?

for example:
    customer.name = "John Smith"
    customer.phone = "555-1212"
    customer.age = 23

could be represented in a sequence:
customer{"John Smith","555-1212",23}

then, if you want to access all of the sequence values, just use
customer, or if you want the age, customer[3].... you can make sequences
even more like structures, style-wise, by using constants to make NAME =
1, PHONE = 2, etc.

Maybe I'm wrong about this...but using sequences as structures works
just fine for me.

Greg
--
Greg Phillips
i.shoot at rednecks.com
http://euphoria.server101.com
--

Useless fact of the day:

Seoul, the South Korean capital, just means "the capital" in the Korean
language

new topic     » goto parent     » topic index » view message » categorize

3. Re: Structures (WAS Re: Memory)

One implementation suggestion would be to create an ALIAS function that
would allow struct like tags to access common sequences.


-----Original Message-----
From: Cox Family <cox.family at SK.SYMPATICO.CA>
To: EUPHORIA at LISTSERV.MUOHIO.EDU <EUPHORIA at LISTSERV.MUOHIO.EDU>
Date: Sunday, January 10, 1999 11:26 AM
Subject: Structures (WAS Re: Memory)


>Dear Euphorians,
>
>Irv Mullins explained:
>
>> >And while I'm at it, can anyone tell me specifically what the term
>> >'structure' means ? Example; is it related to code or data? Please
>> >convert your answear to my 2-digit IQ format smile
>>
>> Both. A structure is a way to "package" variables so they can be
>> handled more conveniently.
>> Example in unEuphoria code:
>>   structure customer
>>      sequence name, addr, city, state, zip, phone
>>      integer age
>>   end structure
>>
>> You could make assignments as follows:
>>    customer.name = "John Smith"
>>    customer.phone = "555-1212"
>>    customer.age = 23
>> and write to disk as puts(fn, customer)
>> or assign the whole thing in one swell foop: thiscustomer = customer
>> which does the same as:
>>   thiscustomer.name = customer.name
>>   thiscustomer.addr = customer.addr
>>   thiscustomer.city = customer.city...and so on.
>
>As Irv pointed out, structures are a way of conveniently handling
>variables. I was surprised when I started learning Euphoria (I still
>am ) that I couldn't find a structure (or struct, or TYPE, depending
>on your background) statement in Euphoria. I would be in favour
>of implementing this into a later Euphoria version, if possible. IMO, it
>doesn't 'dirty up' the code by making it too complex, but rather
>(like stated above) gives the programmer one more way of handling
>variables, and a clean, clear way of doing it at that. Any opinions on
this?
>
>
>Chris Cox
>cox.family at sk.sympatico.ca

new topic     » goto parent     » topic index » view message » categorize

4. Re: Structures (WAS Re: Memory)

On Sun, 10 Jan 1999 13:53:19 -0800, Greg Phillips <i.shoot at REDNECKS.COM>
wrote:

>With Euphoria's sequences, what need is there for structures, really?
>
>for example:
>    customer.name = "John Smith"
>    customer.phone = "555-1212"
>    customer.age = 23
>
>could be represented in a sequence:
>customer{"John Smith","555-1212",23}

It can, however, there are several drawbacks to this workaround:
1. You no longer have type checking.
   Try assigning customer[age] = "Older than Dirt"
   You can do it - not a Good Thing (tm).
2. You will have namespace collisions all over the place.
   Try to find a business program that does _not_ have fields
   like customer.name, supplier.name, and maybe half a dozen other
   "name" or "phone" fields.

>then, if you want to access all of the sequence values, just use
>customer, or if you want the age, customer[3].... you can make sequences
>even more like structures, style-wise, by using constants to make NAME =
>1, PHONE = 2, etc.

It would be easier if Euphoria had the ability to "enum" lists,
but still would not solve the first two problems. As it stands, you
must number and renumber these lists each time you add/delete a "field".
One mistake and kaboom!

Irv

new topic     » goto parent     » topic index » view message » categorize

5. Re: Structures (WAS Re: Memory)

A solution [I've discussed before]:

struct customer
    sequence name
    sequence phone
    integer age
end struct

customer A

A = {"John Smith", "555-1212", 23}

-- becomes:

-- General declarations for this scenario
constant
    new_natural = 0,
    new_char    = 0,
    new_string  = ""

type natural(integer a)
    return (a >= 0)
end type

type char(integer a)
    return (a >= 0 and a <= 255)
end type

type string(sequence n)
    if not length(n) return 1 end if -- empty string legal
    for i = 1 to length(n) do
        if not char(n[i]) then return 0 end if
    end for
    return 1
end type

-- The actual translation:
global constant
    Name  = 1,
    Phone = 2,
    Age   = 3,
    new_customer = {
        new_string,
        new_string,
        new_natural
    }
global type customer(sequence struct)
    string  name
    string  phone
    natural age

    if length(struct) != length(new_customer) return 0 end if

    -- Type check the fields. If any of these fail, they fail in their
    -- respective type checks rather than here...
    name  = struct[Name]
    phone = struct[Phone]
    age   = struct[Age]

    return 1 -- got this far? It's okay then :)
end type

customer A
A = new_customer

A[Name] = "John Smith"
A[Age]  = 23
A[Phone] = "KL5-1A1B" -- A subtype could be set up to define the exact
                      -- specs for a phone number.(char x 3, hyphen, etc.)

-- or:

A = {"John Smith", "KL5-1A1B", 23} -- but this requires knowledge of the
                                   -- structure of 'customer'.

--
Carl R White -- Final Year Computer Science at the University of Bradford
E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :)
URL......: http://www.bigfoot.com/~cyrek/
Ykk rnyllaqur rgiokc cea nyemdok ymc giququezka caysgr -- B.Q.Vgesa

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu