1. Suggestion: declaring multiple interleaved constant sequences

One thing I have long bemoaned is the inability to declare multiple corresponding sequence constants in a clear and simple manner. I usually end up doing something like this:

sequence fields_ = {}, 
         descs_ = {} 
procedure addfield(string field, string desc) 
    fields_ = append(fields_,field) 
    descs_ = append(descs_,desc) 
end procedure 
addfield("signature","(Should be \"Mod\\0\")") 
addfield("size","size (in bytes)") 
constant fields = fields_, 
         descs = descs_ 

I think it would be rather nice if we could just do this:

constant {fields,descs} = {"signature"|"(Should be \"Mod\\0\")", 
                           "size"|"size (in bytes)"} 

The expression on the rhs is just another way of writing

    {{"signature","size"}, 
     {"(Should be \"Mod\\0\")","size (in bytes)"}} 

Obviously such a change would [have to] apply equally to all general [sequence] expression (and sub-expression) handling, not just constant declarations, and of course there would need to be the same number of '|' present in each and every one of the (multi-)element definitions, if you want to avoid a compilation error that is.

Any thoughts? Has this suggestion already been made, and implemented, and if not, why not?blink

Pete

new topic     » topic index » view message » categorize

2. Re: Suggestion: declaring multiple interleaved constant sequences

petelomax said...

One thing I have long bemoaned is the inability to declare multiple corresponding sequence constants in a clear and simple manner. I usually end up doing something like this:

sequence fields_ = {}, 
         descs_ = {} 
procedure addfield(string field, string desc) 
    fields_ = append(fields_,field) 
    descs_ = append(descs_,desc) 
end procedure 
addfield("signature","(Should be \"Mod\\0\")") 
addfield("size","size (in bytes)") 
constant fields = fields_, 
         descs = descs_ 

I think it would be rather nice if we could just do this:

constant {fields,descs} = {"signature"|"(Should be \"Mod\\0\")", 
                           "size"|"size (in bytes)"} 

The expression on the rhs is just another way of writing

    {{"signature","size"}, 
     {"(Should be \"Mod\\0\")","size (in bytes)"}} 

Obviously such a change would [have to] apply equally to all general [sequence] expression (and sub-expression) handling, not just constant declarations, and of course there would need to be the same number of '|' present in each and every one of the (multi-)element definitions, if you want to avoid a compilation error that is.

Any thoughts? Has this suggestion already been made, and implemented, and if not, why not?blink

Pete

Would the columnize function help? Perhaps:

include std/sequence.e 
sequence fields_descs 
fields_descs = columnize( { 
         {"signature","(Should be \"Mod\\0\")"}, 
         -- etc, 
         {"size","size (in bytes)"} 
         } 
constant fields = fields_descs[1], 
         descs = fields_descs[2] 

Of course, it would be helpful if the multiple assignments in v4.1 could be extended to constants:

constant {fields,descs} = columnize(...etc...) 
 
Arthur 
 
new topic     » goto parent     » topic index » view message » categorize

3. Re: Suggestion: declaring multiple interleaved constant sequences

petelomax said...

One thing I have long bemoaned is the inability to declare multiple corresponding sequence constants in a clear and simple manner.

Obviously such a change would [have to] apply equally to all general [sequence] expression (and sub-expression) handling, not just constant declarations,

Any thoughts? Has this suggestion already been made,

Yes. Over ten years ago, now. http://openeuphoria.org/forum/m/62349.wc and http://openeuphoria.org/forum/m/89457.wc

petelomax said...

and implemented, and if not, why not?blink

Parts of it have. http://scm.openeuphoria.org/hg/euphoria/rev/234d97ee08de and http://openeuphoria.org/forum/117157.wc#117157

I don't think it'll work for constants though, but I see no reason to not add that in.

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

4. Re: Suggestion: declaring multiple interleaved constant sequences

What's the issue with coding like this ...

constant  
    fields  = {"signature", `(Should be "Mod\0")`}, 
    descs   = {"size",      "size (in bytes)"}, 
    $ 

[edit]
Ok... now I get it. I've just spent a little more time reading the actual issue and I see your point now.

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

5. Re: Suggestion: declaring multiple interleaved constant sequences

ArthurCrump said...

Would the columnize function help?

It certainly would! Thanks, I consider this suggestion rejected and the problem solved.

Pete

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

6. Re: Suggestion: declaring multiple interleaved constant sequences

petelomax said...

Any thoughts? Has this suggestion already been made, and implemented, and if not, why not?blink

I'm not a fan of your syntax. We do have multi-assignment, though not on declaration. To sort of use your example:

sequence fields, descs 
 
{fields,descs} = {  
    {"signature", "size}, 
    {"(Should be \"Mod\\0\")", "size (in bytes)"} 
  } 
 

That doesn't really do what you're looking for (though Jim already pointed you to columnize()). It's mostly useful when you have a function that returns multiple values.

Matt

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

7. Re: Suggestion: declaring multiple interleaved constant sequences

mattlewis said...

(though Jim already pointed you to columnize())

That wasn't me, but ArthurCrump.

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

8. Re: Suggestion: declaring multiple interleaved constant sequences

jimcbrown said...
mattlewis said...

(though Jim already pointed you to columnize())

That wasn't me, but ArthurCrump.

Oops...

Matt

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

9. Re: Suggestion: declaring multiple interleaved constant sequences

ArthurCrump said...

Of course, it would be helpful if the multiple assignments in v4.1 could be extended to constants:

constant {fields,descs} = columnize(...etc...) 

I just put that into Phix (next release not expected until sometime around autumn 2038 sad).
It was ridiculously easy, really, all it needed was an extra parameter on MultipleAssignment() to prohibit subscripts and add the new symtab entries, which might give someone on the OE team an idea.

You (well, I) can also do things like

constant {opcode,{scale,index,base,offset}} = somefunc(mnemonic,var_id) 

Admittedly that particular example makes more sense when declaring local variables with this syntax.

integer {opcode,{scale,index,base,offset}} = somefunc(mnemonic,var_id) 

Pete

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

10. Re: Suggestion: declaring multiple interleaved constant sequences

petelomax said...
ArthurCrump said...

Would the columnize function help?

It certainly would! Thanks, I consider this suggestion rejected and the problem solved.

Pete

Now to complicate things.

Suppose a constant name is to be applied to each item/index of a multiple interleaved constant sequences? That could be tricky and could resuurect the original requirement.

For example, in win32lib.ew the colour BrightBlue is represented in three ways:

Arthur

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

11. Re: Suggestion: declaring multiple interleaved constant sequences

I intended to add that the constant names could be from an enum

Arthur

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

12. Re: Suggestion: declaring multiple interleaved constant sequences

I'm not sure I follow, well actually I am sure that I don't follow.

I (on Phix) can now happily do this:

function rgb(integer r, integer g, integer b) 
        return r + g * 256 + b * 65536 
end function 
 
constant Black          = rgb(#00, #00, #00), 
         NavyBlue       = rgb(#00, #00, #60), 
         Blue           = rgb(#00, #00, #80), 
         BrightBlue     = rgb(#00, #00, #FF) 
 
constant {w32ColorNames,w32ColorValues} = columnize({{"Black",Black}, 
                                                     {"NavyBlue",NavyBlue}, 
                                                     {"Blue",Blue}, 
                                                     {"BrightBlue",BrightBlue}}) 
 
for i=1 to length(w32ColorNames) do 
    printf(1,"Color %s Value %08x\n",{w32ColorNames[i],w32ColorValues[i]}) 
end for 

And there is no problem whatsoever with Blue/Black/etc being an enum.

You seem to be suggesting that I should be able to do this (yuk):

constant {w32ColorNames,w32ColorValues} = columnize({{"Black",constant Black=rgb(#00, #00, #00)}, 
                                                     {"NavyBlue",constant NavyBlue= rgb(#00, #00, #60)}, 
                                                     {"Blue",constant Blue= rgb(#00, #00, #80)}, 
                                                     {"BrightBlue",constant BrightBlue= rgb(#00, #00, #FF)}}) 

or even worse:

constant {w32ColorNames,w32ColorValues} = columnize({{"Black",      |enum   Black,|}, 
                                                     {"NavyBlue",   |       NavyBlue,|}, 
                                                     {"Blue",       |       Blue,|}, 
                                                     {"BrightBlue", |       BrightBlue|}}) 

Confused,
Pete

Edit: Sorry, I also had this strange "construct" in mind:

constant {w32ColorNames,@={w32ColorValues,{Black,           |= columnize({{"Black",      rgb(#00, #00, #00)}, 
                                           NavyBlue,        |             {"NavyBlue",   rgb(#00, #00, #60)}, 
                                           Blue,            |             {"Blue",       rgb(#00, #00, #80)}, 
                                           BrightBlue}}}    |             {"BrightBlue", rgb(#00, #00, #FF)}}) 

not that I'm suggesting that is doable or desireable either.

Edit2: I should also say that if you are on OpenEuphoria,

constant w32Both = columnize(...), 
         w32ColorNames = w32Both[1], 
         w32ColorValues = w32Both[2] 

is a fairly minor inconvenience

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

Search



Quick Links

User menu

Not signed in.

Misc Menu